我试图将Swagger添加到中,但是我得到了一个404错误。
我的项目设置Spring4.2.5+SpringSecurity4.2.3
pom.xml
<!-- Swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<!-- Swagger-UI -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>SwaggerConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {
@Bean public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo())
.useDefaultResponseMessages(false);
}
/** API Info */
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo("Swagger Sample", "APIs Sample", "Sample Doc 0.1v", "", "Author Name", "This sentence will be display.", "/");
return apiInfo;
}
/** Swagger UI 를 Resource Handler 에 등록 */
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}context-security.xml
<intercept-url pattern="/swagger-ui.html" />
<intercept-url pattern="/swagger-resources" />
<intercept-url pattern="/webjars/** " />
<intercept-url pattern="/v2/api-docs" />


请帮帮我。
发布于 2020-04-22 16:20:08
我希望这能帮到你。我使用Spring,swagger2,而不使用security。
将配置分为2部分
@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("/");
}发布于 2019-12-18 09:57:40
看来,新的待办事项也需要定义几个参数。在我以前的项目中:
@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"));
}如果使用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新参数springfox.documentation.swagger.v2.path=/new/path中。
如果不使用Spring,则使用资源处理程序。
如果这些建议对您没有帮助,我可以用oauth安全访问来描述完全可行的版本。
https://stackoverflow.com/questions/59387582
复制相似问题