Spring swagger 2.6.1/swagger资源/配置/UI404
我试图在Spring框架中添加Swagger,但是我得到了一个404错误 我的项目设置 弹簧4.2.5+弹簧安全4.2.3 pom.xmlSpring swagger 2.6.1/swagger资源/配置/UI404,spring,spring-security,swagger,swagger-ui,swagger-2.0,Spring,Spring Security,Swagger,Swagger Ui,Swagger 2.0,我试图在Spring框架中添加Swagger,但是我得到了一个404错误 我的项目设置 弹簧4.2.5+弹簧安全4.2.3 pom.xml 伊奥·斯普林福克斯 springfox-Swagger 2 2.6.1 伊奥·斯普林福克斯 springfox招摇过市用户界面 2.6.1 SwaggerConfig.java import org.springframework.context.annotation.Bean; 导入org.springframework.context.annotat
伊奥·斯普林福克斯
springfox-Swagger 2
2.6.1
伊奥·斯普林福克斯
springfox招摇过市用户界面
2.6.1
SwaggerConfig.java
import org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.ComponentScan;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
导入org.springframework.web.servlet.config.annotation.WebMVC配置支持;
导入springfox.documentation.builders.PathSelector;
导入springfox.documentation.builders.RequestHandlerSelectors;
导入springfox.documentation.service.apinfo;
导入springfox.documentation.spi.DocumentationType;
导入springfox.documentation.spring.web.plugins.Docket;
导入springfox.documentation.swagger 2.annotations.enableSawagger 2;
@配置
@使能招摇过市2
公共类SwaggerConfig扩展了WebMvcConfigurationSupport{
@Bean公共摘要api(){
返回新摘要(DocumentationType.SWAGGER_2)
.选择()
.api(RequestHandlerSelectors.any())
.path(路径选择器.any())
.build()
.apinfo(apinfo())
.useDefaultResponseMessages(假);
}
/**API信息*/
私有apinfo apinfo(){
ApiInfo ApiInfo=新的ApiInfo(“大摇大摆的样本”、“API样本”、“样本文档0.1v”、“作者姓名”、“将显示这句话。”、“/”;
返回apinfo;
}
/**大摇大摆的用户界面를 资源处理程序에 등록 */
@凌驾
public void addResourceHandlers(ResourceHandlerRegistry注册表){
registry.addResourceHandler(“swagger ui.html”).addResourceLocations(“classpath:/META-INF/resources/”;
addResourceHandler(“/webjars/**”).addResourceLocations(“类路径:/META-INF/resources/webjars/”);
}
}
context security.xml
请帮帮我。看来,新的摘要还需要定义几个参数。在我以前的项目中:
@Bean
public Docket configSwaggerApi() {
return new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.apiInfo(new ApiInfo(environment.getProperty("api.title"), environment.getProperty("api.description"), environment.getProperty("api.version"),
environment.getProperty("api.terms"), new Contact(environment.getProperty("api.contact.name"), environment.getProperty("api.contact.url"),
environment.getProperty("api.contact.mail")), environment.getProperty("api.license"), environment.getProperty("api.license.url")))
.host(environment.getProperty("platform.url"))
.pathMapping(environment.getProperty("server.context-path"))
.protocols(newHashSet("http", "https"))
.produces(Arrays.stream(new String[] {"application/json"}).collect(Collectors.toSet()))
.tags(new Tag("public", "public tools"), new Tag("user","user tools"));
}
如果使用Spring Security,请不要忘记提供对swagger的非安全访问:
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/favicon.ico", "/swagger*", "/swagger.json", "/swagger-resources/**", "/swagger-*/**", "/webjars/**");
}
默认路径/v2/api docs
不需要任何拦截器和配置。如果要更改它,请添加到application.properties
new参数springfox.documentation.swagger.v2.path=/new/path
如果不使用Spring引导,则使用资源处理程序
如果这些建议对您没有帮助,我可以描述使用oauth安全访问的完全可行的版本。我希望这能对您有所帮助。我使用SpringMVC,swagger2,不使用SpringSecurity 将配置分为两部分
@Configuration
@EnableSwagger2
@ComponentScan("com.demo.controller")
public class SwaggerConfig{
@Bean
public Docket apiSwagger(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
接下来,我创建另一类配置
@Configuration
@EnableWebMvc
@Import(SwaggerConfig.class)
public class AppSwaggerConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//enabling swagger-ui part for visual documentation
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
最后,在我的WebAppInitializer类中,在Dispatcher中添加AppSwaggerConfig类
public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(SwaggerConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
try {
loadProperties(rootContext);
} catch (IOException e) {
throw new ServletException("Properties could not be loaded", e);
}
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
dispatcherServlet.register(AppSwaggerConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher",
new DispatcherServlet(dispatcherServlet));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
我把Web.xml servlet操作搞错了