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為關(guān)閉,可以通過變量控制
.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設(shè)置呵恢,所有攔截器都會自動加swagger相關(guān)的資源排除信息
*/
@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();
}
}
//生成全局通用參數(shù)
private List<RequestParameter> getGlobalRequestParameters() {
List<RequestParameter> parameters = new ArrayList<>();
parameters.add(new RequestParameterBuilder()
.name("entId")
.description("企業(yè)號")
.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;
}
//生成通用響應(yīng)信息
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增強(qiáng)
implementation group: 'com.github.xiaoymin', name: 'knife4j-spring-boot-starter', version: '3.0.2'