很多人都是照着别人的文章粘代码,我也是粘的,但是这样粘也会有问题,我搞这个Knife4j3的时候遇到两个问题,这里记录一下:
第一个是basePath丢失,第二个解决basePath丢失完又引发了会引起application/json数据类型参数示例的问题。
在集成 Spring Cloud Gateway 网关的时候,会出现没有 basePath 的情况,例如定义的 /jeeplus-auth、/jeeplus-system 等微服务前缀导致访问接口404:
maven依赖:
swagger2于17年停止维护,现在最新的版本为 Swagger3(Open Api3)
<knife4j.version>3.0.3</knife4j.version>
直接访问是找不到url的:
如果手动添加前缀是可以的
但是每一个接口都要手动添加前缀太繁琐了,也失去了用swagger接口的意义;这时候我们需要在 Gateway 网关添加一个 Filter 过滤器:
GlobalFilter : 不需要在配置文件中配置,作用在所有的路由上,最终通过GatewayFilterAdapter包装成GatewayFilterChain可识别的过滤器,它为请求业务以及路由的URI转换为真实业务服务的请求地址的核心过滤器,不需要配置,系统初始化时加载,并作用在每个路由上。
假设通过网关访问某一微服务的接口URL为:http://网关IP:网关端口/app-user/name/张三该服务使用了swagger3时,且通过网关访问对应接口文档,此时URL则会变为:http://网关IP:网关端口/name/张三请求会缺失bathPath(如果使用服务名动态路由的话,则实际缺失的是对应微服务的applicationName)
在网关模块添加一个过滤器SwaggerGlobalFilter:
网关请求对应微服务接口文档资源时(/v3/api-doc),使用响应拦截,为其响应JSON中添加上对应的bathPath:
过滤器代码:
package com.jeeplus.gateway.filter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Publisher;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;/*** swagger v3/api-docs缺失basePath 过滤器* 解决swagger3 响应缺少bathPath,请求无法动态路由到服务的问题* @Author 955* @Date 2022-09-22 11:31* @Description*/
@Slf4j
@Component
public class SwaggerGlobalFilter implements GlobalFilter, Ordered{public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {ServerHttpRequest request = exchange.getRequest();String path = request.getPath().toString();String host = request.getLocalAddress().getHostString();int port = request.getLocalAddress().getPort();if (!path.endsWith("/v3/api-docs")) {return chain.filter(exchange);}String[] pathArray = path.split("/");System.out.println(pathArray);String basePath = pathArray[1];ServerHttpResponse originalResponse = exchange.getResponse();// 定义新的消息头ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) {@Overridepublic Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {if (super.getStatusCode().equals(HttpStatus.OK) && body instanceof Flux) {Flux<? extends DataBuffer> fluxBody = Flux.from(body);return super.writeWith(fluxBody.buffer().map(dataBuffers -> {List<String> list = new ArrayList<>();dataBuffers.forEach(dataBuffer -> {byte[] content = new byte[dataBuffer.readableByteCount()];dataBuffer.read(content);DataBufferUtils.release(dataBuffer);list.add(new String(content, Charset.forName("UTF-8")));});String s = this.listToString(list);//Feature.DisableSpecialKeyDetect:禁用特殊字符检查JSONObject jsonObject = JSON.parseObject(s, Feature.DisableSpecialKeyDetect);jsonObject.put("host", host + ":" + port);jsonObject.put("basePath", basePath);s = jsonObject.toString();// 设置更新后的header请求头长度int length = s.getBytes().length;HttpHeaders headers = originalResponse.getHeaders();headers.setContentLength(length);return bufferFactory().wrap(s.getBytes(Charset.forName("UTF-8")));}));}return super.writeWith(body);}@Overridepublic HttpHeaders getHeaders() {// 获取父类原始ServerHttpResponse的header请求头信息,这是代理Delegate类型HttpHeaders httpHeaders = super.getHeaders();httpHeaders.set(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8");return httpHeaders;}private String listToString(List<String> list) {StringBuilder stringBuilder = new StringBuilder();for (String s : list) {stringBuilder.append(s);}return stringBuilder.toString();}};// replace response with decoratorreturn chain.filter(exchange.mutate().response(decoratedResponse).build());}@Overridepublic int getOrder() {return -2;}}
重启再次查看发现已经正常了。
测试一下接口:
踩坑:
如果按网上的教程只加过滤器,basePath确实可以显示出来,但是会引发新的问题:
application/json数据类型参数示例丢失
需要在过滤器里面加上这一行:
//Feature.DisableSpecialKeyDetect:禁用特殊字符检查JSONObject jsonObject = JSON.parseObject(s, Feature.DisableSpecialKeyDetect);
再次启动查看:
都正常了。