背景
在spring boot中使用Swagger3,当访问http://localhost:8080/swagger-ui/index.html时报【Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway】
原因
出现这个弹窗的原因是swagger接口返回的数据格式和期望的不一致,可以使用http://localhost:8080/v3/api-docs
查看返回的格式,很明显是不符合swagger的数据要求。
正确的数据格式如下
造成这个结果的原因是项目加了统一结果转换,如下
@RestControllerAdvice(annotations = RestController.class)
public class GlobalResponseAdvice implements ResponseBodyAdvice<Object> {
}
解决方法
可以通过在RestControllerAdvice的注解上指定转换项目包,不转换swagger的
@RestControllerAdvice(basePackages = "org.moss")
public class GlobalResponseAdvice implements ResponseBodyAdvice<Object> {
}
最后可以访问了