关于nginx:swaggerui-修改默认的请求地址

87次阅读

共计 1096 个字符,预计需要花费 3 分钟才能阅读完成。

问题

将 Springboot 利用部署到服务器上 通过域名拜访 swagger-ui(通过了 Nginx 代理)进行前后端联调 然而理论点击执行的时候 提醒:TypeError: Failed to fetch

如 swagger-ui 拜访地址是:

https://foo.com/test/api/insurance/swagger-ui/index.html

点击执行 调用后端接口的地址 变成了

http://foo.com:80/solvStaInfos

因为通过 Nginx 代理 理论后端地址应该是

https://foo.com/test/api/insurance/solvStaInfos

解决

swagger-ui 页面上地址取自接口:/v3/api-docs中的返回

servers: [
    {
        url: "http://foo.com:80",
        description: "Inferred Url"
    }
],

批改此地址为 https://foo.com/test/api/insurance 即可

办法一

Nginx 动静批改接口返回内容

location /test/api/insurance/v3/api-docs {
    sub_filter 'http://foo.com:80' 'https://foo.com/test/api/insurance/';
    sub_filter_types application/json;
    proxy_pass ...;
}

注:须要 Nginx 反对 即蕴含对应的 module

办法二

代码层面批改

@Component
public class SpringfoxSwaggerHostResolver implements WebMvcOpenApiTransformationFilter {
    
    @Override
    public OpenAPI transform(OpenApiTransformationContext<HttpServletRequest> context) {OpenAPI swagger = context.getSpecification();
        Server server = new Server();
        server.setUrl("https://foo.com/test/api/insurance/");
        swagger.setServers(Arrays.asList(server));
        return swagger;
    }

    @Override
    public boolean supports(DocumentationType delimiter) {return DocumentationType.OAS_30.equals(delimiter);
    }
}

正文完
 0