swagger-ui.html页面无法打开解决方案

1.@EnableWebMvc注解必须去掉。

2.实现WebMvcConfigurer,并添加如下代码

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
  registry.addResourceHandler("swagger-ui.html")
      .addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
  registry.addResourceHandler("/webjars/**")
      .addResourceLocations("classpath:/META-INF/resources/webjars/");
}

3.检查配置文件application.properties

1) 将如下配置去掉,或者改成true:

spring.resources.add-mappings=false

2) 将如下配置去掉:

spring.resources.static-locations: classpath:/webapp/

4.查看nginx配置

检查前端代码,发现nginx配置有如下一段:

location /api/swagger-ui.html{
        proxy_pass http://server-service:8080/;
        proxy_set_header X-Real-IP $remote_addr;
        client_max_body_size    1000m;
        }

由于当前应用部署在k8s环境中,后端的api地址恰好为http://server-service:8080/,所以这个配置会导致请求直接转发到后端接口,导致无法访问页面。

事实上,针对swagger,前端nginx不需要做任何映射,直接请求 http:// {后端公网host}/swagger-ui.html 即可访问。 

参考:

https://github.com/springfox/springfox/issues/1460
https://github.com/springfox/springfox/issues/2037
https://github.com/springfox/springfox/issues/2396

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值