1.加入依赖
compile group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'
2.swagger配置类
@Configuration
public class SwaggerConfiguration implements WebMvcConfigurer {
private static final String API_BASE_PACKAGE = "com.romens.market.webapp";
@Autowired
private SwaggerConfigProperties configProperties;
@Bean
public Docket createRestApi() {
Docket docket = new Docket(DocumentationType.OAS_30)
// 定义是否开启swagger,false为关闭,可以通过变量控制
.enable(configProperties.isEnable())
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage(API_BASE_PACKAGE))
.paths(PathSelectors.any())
.build()
.globalRequestParameters(getGlobalRequestParameters())
.directModelSubstitute(org.joda.time.LocalDate.class, java.sql.Date.class)
.directModelSubstitute(org.joda.time.DateTime.class, java.util.Date.class);
if (!StringUtils.isEmpty(configProperties.getPrefix())) {
docket.pathMapping(configProperties.getPrefix());
}
return docket.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
Contact contact = new Contact("项目组",
"http://www./",
"xx@xx.cn");
return new ApiInfoBuilder()
.title("webapp-service")
.description("模块")
.termsOfServiceUrl("")
.contact(contact)
.version("1.0.0")
.build();
}
/**
* 通用拦截器排除swagger设置,所有拦截器都会自动加swagger相关的资源排除信息
*/
@SuppressWarnings("unchecked")
@Override
public void addInterceptors(InterceptorRegistry registry) {
try {
Field registrationsField = FieldUtils.getField(InterceptorRegistry.class, "registrations", true);
List<InterceptorRegistration> registrations = (List<InterceptorRegistration>) ReflectionUtils.getField(registrationsField, registry);
if (registrations != null) {
for (InterceptorRegistration interceptorRegistration : registrations) {
interceptorRegistration
.excludePathPatterns("/swagger**/**")
.excludePathPatterns("/webjars/**")
.excludePathPatterns("/v3/**")
.excludePathPatterns("/doc.html");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//生成全局通用参数
private List<RequestParameter> getGlobalRequestParameters() {
List<RequestParameter> parameters = new ArrayList<>();
parameters.add(new RequestParameterBuilder()
.name("entId")
.description("企业号")
.required(true)
.in(ParameterType.HEADER)
.query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
.required(false)
.build());
parameters.add(new RequestParameterBuilder()
.name("operator")
.description("操作人")
.required(false)
.in(ParameterType.HEADER)
.query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
.required(false)
.build());
return parameters;
}
//生成通用响应信息
private List<Response> getGlobalResonseMessage() {
List<Response> responseList = new ArrayList<>();
responseList.add(new ResponseBuilder().code("404").description("找不到资源").build());
return responseList;
}
}
3.添加资源映射(重要)
@Configuration
public class RomensWebMvcConfigurationSupport extends WebMvcConfigurationSupport {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
registry.addResourceHandler("favicon.ico")
.addResourceLocations("classpath:/static/favicon.ico");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
补充
1)swagger增强
implementation group: 'com.github.xiaoymin', name: 'knife4j-spring-boot-starter', version: '3.0.2'