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

时间:2016-08-31 10:46:06

标签: spring rest 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());
}
}

网络配置文件

@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/");
}

}

我确实为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();

}

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

  

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&GT;   springfox.documentation.swagger.web.ApiResourceController.swaggerResources()   2016-08-31 11:24:55 INFO [localhost-startStop-1]   RequestMappingHandlerMapping - 映射   “{ [/ swagger-resources / configuration / ui] }”   org.springframework.http.ResponseEntity   springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()

1 个答案:

答案 0 :(得分:2)

假设您正在密切关注该指南(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>