我一直试图用我的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(ListMeta-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()
尝试更改springfox-swagger-ui依赖项的版本以匹配springfox-swagger2.
我使用了以下内容(该指南的swagger-ui为2.4.0):