【问题标题】:swagger 2.6.1 /swagger-resources/configuration/ui 404招摇 2.6.1 /swagger-resources/configuration/ui 404
【发布时间】:2019-12-18 07:49:04
【问题描述】:

我正在尝试将 Swagger 添加到 Spring Framework,但出现 404 错误。

我的项目设置 Spring 4.2.5 + Spring Security 4.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" />

请帮帮我。

【问题讨论】:

    标签: spring spring-security swagger swagger-ui swagger-2.0


    【解决方案1】:

    我希望这可以帮助你。我使用 Spring Mvc、swagger2 并且不使用 spring security。

    将配置分成两部分

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

    【讨论】:

      【解决方案2】:

      我似乎,新的 Docket 还需要定义一些参数。在我的旧项目中:

      @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新参数springfox.documentation.swagger.v2.path=/new/path即可。

      如果您不使用 Spring Boot,则使用资源处理程序。

      如果这些建议对您没有帮助,我可以描述具有 oauth 安全访问权限的完全可行的版本。

      【讨论】:

      • 我把 Web.xml servlet 操作弄错了 .do
      猜你喜欢
      • 2018-06-11
      • 2016-12-09
      • 2020-10-23
      • 2014-10-15
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多