当前位置:   article > 正文

Springboot集成swagger3_springboot3.2 swagger 3.0

springboot3.2 swagger 3.0

springboot集成swagger3.0遇到的坑

  1. 导入依赖,swagger3.0版本之前需要导入两个依赖springfox-swagger2和springfox-swagger-ui,3.0版本后只需要导入springfox-boot-starter
		<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  1. 添加配置类SwaggerConfig,swagger3.0版本之前使用@EnableSwagger2,3.0版本后使用@EnableOpenApi
@Configuration
@EnableOpenApi
public class Swagger2Config{
  @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.com.eshell.signal.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("项目接口文档html")
                .description("www.baidu.com")
                .version("1.0")
                .build();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  1. 启动访问lochost:8080/swagger-ui.html会出现404,这个问题我也是找了好久,才发现了各种问题。
  • swagger3.0默认路径不是swagger-ui.html,改为ip+port/swagger-ui/index.html
  • 如果项目使用了拦截器,需要在配置一下,可以在原有的拦截器下添加或者在swagger配置类继承WebMvcConfigurationSupport 重写addResourceHandlers方法,代码如下:
 /**
     * 解决swagger被拦截的问题
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        // 解决静态资源无法访问
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
        // 解决swagger无法访问
        registry.
                addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                .resourceChain(false);
        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/swagger-ui/")
                .setViewName("forward:/swagger-ui/index.html");
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/939928
推荐阅读
相关标签
  

闽ICP备14008679号