首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >swagger 2.6.1 /swagger-resources/配置/ui 404

swagger 2.6.1 /swagger-resources/配置/ui 404
EN

Stack Overflow用户
提问于 2019-12-18 07:49:04
回答 2查看 7.6K关注 0票数 1

我试图将Swagger添加到中,但是我得到了一个404错误。

我的项目设置Spring4.2.5+SpringSecurity4.2.3

pom.xml

代码语言:javascript
复制
<!-- 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

代码语言:javascript
复制
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

代码语言:javascript
复制
<intercept-url pattern="/swagger-ui.html" />
<intercept-url pattern="/swagger-resources" />
<intercept-url pattern="/webjars/** " />
<intercept-url pattern="/v2/api-docs" />

请帮帮我。

EN

回答 2

Stack Overflow用户

发布于 2020-04-22 16:20:08

我希望这能帮到你。我使用Spring,swagger2,而不使用security。

将配置分为2部分

代码语言:javascript
复制
 @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();
        }

    }

接下来,我将创建另一类配置。

代码语言:javascript
复制
@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类

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2019-12-18 09:57:40

看来,新的待办事项也需要定义几个参数。在我以前的项目中:

代码语言:javascript
复制
@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的非安全访问:

代码语言:javascript
复制
@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安全访问来描述完全可行的版本。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59387582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档