springboot学习(三十三) 升级至swagger3.0

当前swagger已经升级为3.0.0,主要更新如下:
Spring 5,Webflux 支持(仅请求映射支持,尚不支持功能端点)
Spring Integration 支持
Spring Boot 支持 springfox-boot-starter 依赖性(零配置,自动配置支持)
具有自动完成功能的文档化配置属性
更好的规范兼容性
支持 OpenApi 3.0.3
几乎零依赖性(唯一需要的库是 spring-plugin、pswagger-core)
现有的 swagger2 注释将继续有效,并丰富 open API 3.0 规范

1 添加3.0的依赖
swaggerVersion定义的版本号为3.0.0

compile group: 'io.springfox', name: 'springfox-boot-starter', version:"${swaggerVersion}"

如果是maven

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
<dependency>

2 配置
去除swagger2的EnableSwagger2注解,使用@EnableOpenApi

package com.iscas.biz.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * swagger配置
 *
 * @author zhuquanwen
 * @vesion 1.0
 * @date 2020/08/28
 * @since jdk1.8
 */
@Configuration
@EnableOpenApi
public class Swagger3Config {
     
    @Value("${swagger.enable: true}")
    private boolean swaggerEnable;
    @Bean
    public Docket api() {
     
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .enable(swaggerEnable)
                .select()
                // 自行修改为自己的包路径
//                .apis(RequestHandlerSelectors.basePackage("com.iscas.biz.test.controller"))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
     
        return new ApiInfoBuilder()
                .title("swagger3-文档")
                .description("")
                //服务条款网址
                //.termsOfServiceUrl("http://blog.csdn.net/forezp")
                .version("1.0")
                //.contact(new Contact("帅呆了", "url", "email"))
                .build();
    }
}

3 添加静态资源配置

package com.iscas.biz.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * //TODO
 *
 * @author zhuquanwen
 * @vesion 1.0
 * @date 2020/8/28 21:02
 * @since jdk1.8
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
     

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
     
        registry.
                addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                .resourceChain(false);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
     
        registry.addViewController("/swagger-ui/")
                .setViewName("forward:/swagger-ui/index.html");
    }
}


4 访问
地址换了,不是swagger-ui.html了

http://<ip>:<port>/<contenxtPath>/swagger-ui/

你可能感兴趣的:(spring,boot,swagger,spring,boot,swagger3,swagger,swagger升级,springfox)