本地环境代码
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
引入swagger(注意所有子模块包括gateway都要引入此依赖)
<!-- 可以在parent中编写此配置,子项目直接引用依赖无需设置版本 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
网关配置
- gateway依赖配置
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<!-- 使用parent的版本 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-webflux-ui</artifactId>
<version>1.6.14</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-ui</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations-jakarta</artifactId>
<version>2.2.7</version>
</dependency>
- 编写配置类 SwaggerUiWebFluxConfigurer
@Configuration
public class SwaggerUiWebFluxConfigurer implements WebFluxConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
// 配置跨域
registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "OPTION", "DELETE").maxAge(3600);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 重新映射路径
registry.
addResourceHandler("/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
.resourceChain(false);
registry.addResourceHandler("/swagger-ui.html**")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/").resourceChain(true);
}
}
- 重新设置访问路径
SwaggerHandler
@RestController
public class SwaggerHandler {
@Autowired(required = false)
private SecurityConfiguration securityConfiguration;
@Autowired(required = false)
private UiConfiguration uiConfiguration;
private final SwaggerResourcesProvider swaggerResources;
@Autowired
public SwaggerHandler(SwaggerResourcesProvider swaggerResources) {
this.swaggerResources = swaggerResources;
}
@GetMapping("/swagger-resources/configuration/security")
public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {
return Mono.just(new ResponseEntity<>(
Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));
}
@GetMapping("/swagger-resources/configuration/ui")
public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
return Mono.just(new ResponseEntity<>(
Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
}
@GetMapping("/swagger-resources")
public Mono<ResponseEntity> swaggerResources() {
return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK)));
}
}
- 启动类添加注解
@EnableSwagger2WebFlux
@SpringBootApplication
@EnableSwagger2WebFlux
public class ImesGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ImesGatewayApplication.class, args);
}
}
5.配置引用的服务 在bootstrap.yaml
或application.yaml
编写
springdoc:
swagger-ui:
urls:
- name: '你自己的服务名称1'
url: 服务路径前缀/v2/api-docs
- name: '你自己的服务名称2'
url: 服务路径前缀/v2/api-docs
其他服务配置
- pom.xml 引用
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
- 配置类(我的服务比较特殊需要实现过滤器转发请求后才能访问)
注入 Docket 的bean是必须的
@Configuration
public class SwaggerFilter implements Filter {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("填写你要扫描的包名"))
.paths(PathSelectors.any())
.build()
.globalRequestParameters(getGlobalRequestParameters());
}
//生成全局通用参数
private List<RequestParameter> getGlobalRequestParameters() {
List<RequestParameter> parameters = new ArrayList<>();
parameters.add(new RequestParameterBuilder()
.name("Authorization")
.description("认证")
.in(ParameterType.HEADER)
.query(q -> q.model(m -> m.scalarModel(ScalarType.STRING))
.defaultValue("Bearer "))
.required(true)
.build());
return parameters;
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
if (httpRequest.getRequestURI().equals("/device/v2/api-docs")) {
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/v2/api-docs");
requestDispatcher.forward(request,response);
}else {
chain.doFilter(request,response);
}
}
@Override
public void destroy() {
Filter.super.destroy();
}
}
- 启动类设置
@EnableSwagger2
@SpringBootApplication
@EnableSwagger2
public class ImesDeviceApplication {
public static void main(String[] args) {
SpringApplication.run(ImesDeviceApplication.class, args);
}
}
注意:如果有配置安全认证相关的代码需要放行
"/v2/api-docs"
路径
完成!访问 http://你的ip+端口/doc.html
即可进入