当前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 规范
- Maven pom依赖
注:此依赖可以不再使用
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>-->
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
改为直接使用springfox-boot-starter依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
2、配置
去除swagger2的EnableSwagger2注解,使用@EnableOpenApi
package com.imooc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.oas.annotations.EnableOpenApi;
/**
* @author pei
* @version 1.0
* @date 2020/6/15 9:05 下午
* @Description spring 启动类
* http://localhost:8080/swagger-ui/index.html
*/
@RestController
@SpringBootApplication
@EnableOpenApi
public class DemoApplication {
/**
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
3、配置
添加静态资源配置package com.iscas.biz.config;
package com.imooc.web.config;
import com.imooc.web.filter.TimeFilter;
import com.imooc.web.interceptor.TimeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.List;
/**
* @author pgw
* @version 1.0
* @date 2020/10/19 7:50 下午
* @Description
*/
@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、访问
http://localhost:8080/swagger-ui/index.html