无法使用弹簧启动加载Swagger-UI

问题描述 投票:1回答:1

我正在尝试使用Swagger动态记录我的Spring Boot应用程序REST API。

为了做到这一点,我使用springfox和它的swagger集成。我遵循官方文档,可以是found here

完成集成后,我可以通过http://localhost:8080/v2/api-docs访问JSON输出,但是我无法通过http://localhost:8080/swagger-ui.html在Swagger UI中看到任何信息。

这是我的设置:

pom.hml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <properties>
        <java.version>1.8</java.version>
        <main.basedir>${basedir}/../..</main.basedir>
        <ing.continuous-delivery.version>00.04.04</ing.continuous-delivery.version>
        <maven.assembly.version>2.3</maven.assembly.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.5.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

        <!-- tag::spring -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>1.5.3.RELEASE</version>
        </dependency>
        <!-- end::spring -->

        <!-- tag::web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-hateoas</artifactId>
            <version>1.5.3.RELEASE</version>
        </dependency>
        <!-- end::web -->

        <!--  tag::swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>
        <!--  end:: swagger -->
    </dependencies>

</project>

Web MVC配置:

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(false);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}

安全配置:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeRequests().antMatchers("/**", "/swagger-resources").permitAll();
        httpSecurity.csrf().disable();
        httpSecurity.headers().frameOptions().disable();
    }
}

Swagger配置:

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any()).build();
    }
}

谢谢您的帮助!

spring-boot swagger swagger-ui swagger-2.0 springfox
1个回答
-1
投票

在尝试访问swagger UI时,您需要在/swagger-ui.html之前添加API的基本URL。

像这样的东西:

http://localhost:8080/nameOfAPI/swagger-ui.html

这篇文章帮助了我:https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

© www.soinside.com 2019 - 2024. All rights reserved.