SpringCloudGateway网关整合swagger3+Knife4j3,basePath丢失请求404问题

在集成 Spring Cloud Gateway 网关的时候,会出现没有 basePath 的情况,例如定义的 /jeeplus-auth、/jeeplus-system 等微服务前缀导致访问接口404:

maven依赖:

swagger2于17年停止维护,现在最新的版本为 Swagger3(Open Api3)

3.0.3

SpringCloudGateway网关整合swagger3+Knife4j3,basePath丢失请求404问题_第1张图片

直接访问是找不到url的:

SpringCloudGateway网关整合swagger3+Knife4j3,basePath丢失请求404问题_第2张图片

如果手动添加前缀是可以的

SpringCloudGateway网关整合swagger3+Knife4j3,basePath丢失请求404问题_第3张图片
SpringCloudGateway网关整合swagger3+Knife4j3,basePath丢失请求404问题_第4张图片
但是每一个接口都要手动添加前缀太繁琐了,也失去了用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:
SpringCloudGateway网关整合swagger3+Knife4j3,basePath丢失请求404问题_第5张图片

过滤器代码:

package com.jeeplus.gateway.filter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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 过滤器
 *
 * @Author 955
 * @Date 2022-09-22 11:31
 * @Description
 */
@Slf4j
@Component
public class SwaggerGlobalFilter  implements GlobalFilter, Ordered{
    public Mono 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) {
            @Override
            public Mono writeWith(Publisher body) {
                if (super.getStatusCode().equals(HttpStatus.OK) && body instanceof Flux) {
                    Flux fluxBody = Flux.from(body);
                    return super.writeWith(fluxBody.buffer().map(dataBuffers -> {
                        List 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);
                        JSONObject jsonObject = JSON.parseObject(s);

                        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);
            }

            @Override
            public 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 list) {
                StringBuilder stringBuilder = new StringBuilder();
                for (String s : list) {
                    stringBuilder.append(s);
                }
                return stringBuilder.toString();
            }
        };

        // replace response with decorator
        return chain.filter(exchange.mutate().response(decoratedResponse).build());
    }

    @Override
    public int getOrder() {
        return -2;
    }

}

重启再次查看发现已经正常了。

SpringCloudGateway网关整合swagger3+Knife4j3,basePath丢失请求404问题_第6张图片

测试一下接口:

SpringCloudGateway网关整合swagger3+Knife4j3,basePath丢失请求404问题_第7张图片

你可能感兴趣的:(Java,常见工具类,常见错误解决方法,spring,cloud,gateway,Knife4j,Swagger3.0)