使用spring boot整合swagger2,访问http://localhost:8080/swagger-ui.html#/时出现错误。
Unable to infer base url. This is common when using dynamic servlet registration or when the API
is behind an API Gateway. The base url is the root of where all the swagger resources are served.
For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.
org/api/. Please enter the location manually:
解决方法:
在spring boot的启动类上加入
@ComponentScan(basePackages = {"com.zhongyue.examination.*","com.zhongyue.common.*","cc.*","com.zhongyue.common.config"})
具体到你配置SwaggerConfig配置类的包名,不要放在@SpringBootApplication的包扫描,我一开始就是放在@SpringBootApplication中
如果是spring boot整合shiro的,还需要在shiro中放行
如下:
//放行swagger2
filterChainDefinitionMap.put("/swagger-ui.html", "anon");
filterChainDefinitionMap.put("/swagger-resources/**", "anon");
filterChainDefinitionMap.put("/v2/api-docs/**", "anon");
filterChainDefinitionMap.put("/webjars/springfox-swagger-ui/**", "anon");