Spring应用程序:为什么swagger-ui无法在swagger-resources/configuration/ui上执行HTTP GET?

ser*_*rah 6 rest spring swagger swagger-ui swagger-2.0

我一直试图用我的Spring REST应用程序设置swagger-ui.我使用的是弹簧4.2.6.RELEASE和swagger core&ui 2.5.0.我没有使用昂首阔步的注释,期望能够大摇大摆地获取Spring REST注释.

我可以大摇大摆来生成api文档,我可以在v2/api-docs下查看它.

我能够点击swagger-ui.html页面,但它没有在页面上显示任何api-docs或控制器信息.在浏览器上启用调试器模式时,我发现在尝试获取"swagger-resources/configuration/ui"时出错是错误的 - 返回404(未找到).

我按照以下链接设置了swagger-ui http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

我最初设置了上面链接中指定的资源处理程序,但这也没有帮助,并给了我相同的404错误.我已经尝试调整资源处理程序,所以看看它是否有助于swagger-ui对swagger-resources/configuration/ui进行GET.

为什么swagger-ui无法获取资源swagger-resources/configuration/ui?

我已经设置了我的资源处理程序,如下所示.

SwaggerConfiguration

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

@Bean
public Docket api(){
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build();
            //.apiInfo(apiInfo());
}
}
Run Code Online (Sandbox Code Playgroud)

Web配置文件

@EnableWebMvc
@Configuration
@Import(SwaggerConfiguration.class)
@ComponentScan("com.bank.direct.services")

public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> pConverters) {
    pConverters.add(RestUtils.getJSONMessageConverter());
}

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

}

我确实为webapp设置了SecurityConfig,但是我已经将它保持在最低限度,以防它可能导致任何问题.

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {


@Autowired
private AuthenticationService _authenticationService;


@Autowired
public void globalUserDetails(AuthenticationManagerBuilder pAuth) throws Exception {

    pAuth.userDetailsService(_authenticationService);
}


@Override
protected void configure(HttpSecurity pHttp) throws Exception {

    // Enable HTTP caching
    pHttp.headers().cacheControl().disable();

    // Configure security
    pHttp.httpBasic()

    // -- Allow unauthenticated request (must be done before allowing only authenticated requests)
    .and()
    .authorizeRequests()
    .antMatchers("/rest/application/information/").permitAll();

}
Run Code Online (Sandbox Code Playgroud)

我确实在应用程序启动时看到了一些资源映射

2016-08-31 11:24:55 INFO [localhost-startStop-1] RequestMappingHandlerMapping - 映射"{[/ v2/api-docs],methods = [GET],产生= [application/json || application/hal + json]}"on public org.springframework.http.ResponseEntity springfox.documentation.swagger2.web.Sw​​agger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)2016-08-31 11:24:55 INFO [localhost-startStop-1] RequestMappingHandlerMapping - 将"{[/ swagger-resources/configuration/security]}"映射到org.springframework.http.ResponseEntity springfox.documentation.swagger.web.ApiResourceController.securityConfiguration()2016-08-31 11:24:55 INFO [localhost-startStop-1] RequestMappingHandlerMapping - 将"{[/ swagger-resources]}"映射到org.springframework.http.ResponseEntity> springfox.documentation.swagger.web.ApiResourceController.swaggerResources()2016- 08-31 11:24:55 INFO [localhost-startStop-1] RequestMappingHandlerMapping - 将"{ [/ swagger-resources/configuration/ui] }" 映射到org.springfram ework.http.ResponseEntity springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()

小智 3

假设您密切关注该指南(http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api),您最终会得到不同版本的 swagger 依赖项,这似乎会导致404(至少对我来说......)

尝试更改 springfox-swagger-ui 依赖项的版本以匹配 springfox-swagger2。

我使用了以下内容(指南中的 swagger-ui 为 2.4.0):

<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>
Run Code Online (Sandbox Code Playgroud)