当用nginx通过路径区分接口的时候,前端调用接口的时候需要加上这个路径{host}/api/{接口}
1 2 3
| location ^~ /api/ { proxy_pass http://10.200.195.1:9999/; }
|
此时访问swagger{host}/api/swagger-ui.html
的时候,不会自动在接口加上api
,导致Try it out
的时候404。
下面有几种方法,能解决此问题,可以选择适合自己的使用。
修改BASE URL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @Autowired private ServletContext servletContext;
@Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .pathProvider(new RelativePathProvider(servletContext) { @Override public String getApplicationBasePath() { return "/api"; } }) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.test")) .paths(PathSelectors.any()) .build(); }
|
url加前缀
1 2 3 4 5 6 7 8 9 10 11
| @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .pathMapping("/api") .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.test")) .paths(PathSelectors.any()) .build(); }
|
覆盖默认host
在springboot配置文件中添加如下配置:
1 2 3 4 5
| springfox: documentation: swagger: v2: host: localhost:8080/api
|