Springfox(Swagger3)在SpringBoot中使用
问题将SpringBoot服务部署到开发环境的后,使用开发环境的网关访问Swagger时,发现Swagger页面能够正常打开,但是Swagger调试的接口请求全部都报404问题。原因主要原因就是nginx网关上面又一个前缀转发的url,但是Swagger页面的Http客户端不知道这个前缀url,所以,需要告诉Swagger网关配置的url前缀是什么。解决Maven<dependency>
·
问题
将SpringBoot服务部署到开发环境的后,使用开发环境的网关访问Swagger时,发现Swagger页面能够正常打开,但是Swagger调试的接口请求全部都报404问题。
原因
主要原因就是nginx网关上面又一个前缀转发的url,但是Swagger页面的Http客户端不知道这个前缀url,所以,需要告诉Swagger网关配置的url前缀是什么。
解决
Maven
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Swagger.java
package com.xxx.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class Swagger {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())//调用apiInfo方法,创建一个ApiInfo实例,里面是展示在文档页面信息内容
.select()
//控制暴露出去的路径下的实例
//如果某个接口不想暴露,可以使用以下注解
//@ApiIgnore 这样,该接口就不会暴露在 swagger2 的页面下
.apis(RequestHandlerSelectors.basePackage("com.xxx.controller"))
.paths(PathSelectors.any())
.build()
// 网关前缀
.pathMapping("/api");
}
//构建 api文档的详细信息函数
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("xxxapi文档")
.description("xxxapi文档")
.termsOfServiceUrl("http://www.baidu.com")
.version("1.0")
.build();
}
}
WebMvcConfig.java
package com.xxx.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.WebContentInterceptor;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Value("${spring.profiles.active}")
private String activeProfile;
@Override
public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration interceptorRegistration =
registry
.addPathPatterns("/**")
// 需要排除的URL,例如登录请求
.excludePathPatterns("/user/login");
// 再哪些环境中需要启用swagger
if ("dev".equals(activeProfile)
|| "test".equals(activeProfile)) {
// swagger-ui
interceptorRegistration
.excludePathPatterns(
"/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
}
WebContentInterceptor interceptor = new WebContentInterceptor();
interceptor.addCacheMapping(CacheControl.noCache().cachePrivate().noTransform(), "/**");
registry.addInterceptor(interceptor);
}
}
效果
使用连接http://127.0.0.1:8080/swagger-ui/
打开即可。
参考
- springfox/springfox
- SpringBoot 整合 Swagger2
更多推荐
已为社区贡献10条内容
所有评论(0)