微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我一直试图用我的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());
}
}

Web配置文件

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

public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void configureMessageConverters(Listsspath:/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 – Mapped
“{[/v2/api-docs],methods=[GET],produces=[application/json ||
application/hal+json]}” onto public
org.springframework.http.ResponseEntity
springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)
2016-08-31 11:24:55 INFO [localhost-startStop-1]
RequestMappingHandlerMapping – Mapped
“{[/swagger-resources/configuration/security]}” onto
org.springframework.http.ResponseEntity
springfox.documentation.swagger.web.ApiResourceController.securityConfiguration()
2016-08-31 11:24:55 INFO [localhost-startStop-1]
RequestMappingHandlerMapping – Mapped “{[/swagger-resources]}” onto
org.springframework.http.ResponseEntity>
springfox.documentation.swagger.web.ApiResourceController.swaggerResources()
2016-08-31 11:24:55 INFO [localhost-startStop-1]
RequestMappingHandlerMapping – Mapped
“{[/swagger-resources/configuration/ui]}” onto
org.springframework.http.ResponseEntity
springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐