集成swagger
springboot集成
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-ui</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!--swagger-->
@Configuration
@EnableOpenApi
@ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true)
@EnableAutoConfiguration
public class SwaggerConfig {
@Bean
@ConditionalOnMissingBean
public SwaggerProperties swaggerProperties(){
return new SwaggerProperties();
}
@Bean
public Docket docket(SwaggerProperties swaggerProperties){
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo(swaggerProperties))
.select()
.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
.paths(PathSelectors.regex("/error.*").negate())
.paths(PathSelectors.any())
.build().globalRequestParameters(parameters());
}
private ApiInfo apiInfo(SwaggerProperties swaggerProperties) {
return new ApiInfoBuilder().title(swaggerProperties.getTitle()).description(swaggerProperties.getDescription()).version(swaggerProperties.getVersion()).build();
}
private List<RequestParameter> parameters(){
List<RequestParameter> globalRequestParamters = new ArrayList<>();
RequestParameter requestParameter = new RequestParameterBuilder()
.name("authorization")
.description("test")
.in(ParameterType.HEADER)
.required(false)
.build();
globalRequestParamters.add(requestParameter);
return globalRequestParamters;
}
}
@Data
@Component
@ConfigurationProperties("swagger")
public class SwaggerProperties {
private String title;
private String description;
private String version;
private String basePackage;
public String getTitle() {
return title;
}
public SwaggerProperties setTitle(String title) {
this.title = title;
return this;
}
public String getDescription() {
return description;
}
public SwaggerProperties setDescription(String description) {
this.description = description;
return this;
}
public String getVersion() {
return version;
}
public SwaggerProperties setVersion(String version) {
this.version = version;
return this;
}
public String getBasePackage() {
return basePackage;
}
public SwaggerProperties setBasePackage(String basePackage) {
this.basePackage = basePackage;
return this;
}
}
swagger.enabled=true
swagger.basePackage=com.fwpt.modules.system.controller
swagger.description=system
swagger.title=system
获取api清单
@RestController
public class ApiCollectController {
@Autowired
WebApplicationContext webApplicationContext;
@Autowired
RequestMappingHandlerMapping mapping;
@ApiIgnore
@RequestMapping(value = "/getAllUrls",method = RequestMethod.GET)
public List<Map<String,Object>> getAllUrls(){
List<Map<String,Object>> list = new ArrayList<>();
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = mapping.getHandlerMethods();
for(Map.Entry<RequestMappingInfo,HandlerMethod> entry : handlerMethodMap.entrySet()){
String className = entry.getValue().getMethod().getDeclaringClass().getName();
if(ObjectUtils.isEmpty(className) || className.contains("SwaggerConfig")){
continue;
}
Map<String,Object> map = new HashMap<>(16);
map.putAll(getSwaggerProperties(entry.getValue()));
map.put("requestType", getRequestType(entry.getKey().getMethodsCondition()));
map.put("requestServer", webApplicationContext.getEnvironment().getProperty("spring.application.name"));
PatternsRequestCondition patternsCondition = entry.getKey().getPatternsCondition();
for (String url : patternsCondition.getPatterns()) {
map.put("requestUrl", getRequestUrl(url));
list.add(map);
}
}
return list;
}
private Map<String, Object> getSwaggerProperties(HandlerMethod handlerMethod) {
Map<String, Object> map = new HashMap<>(16);
String requestName = "";
String remark = "";
Api api = handlerMethod.getMethod().getDeclaringClass().getAnnotation(Api.class);
if (handlerMethod.getMethod().getDeclaringClass().isAnnotationPresent(Api.class) && Objects.nonNull(api)) {
requestName = api.value();
}
ApiOperation apiOperation = handlerMethod.getMethodAnnotation(ApiOperation.class);
if (handlerMethod.hasMethodAnnotation(ApiOperation.class) && Objects.nonNull(apiOperation)) {
requestName = ObjectUtils.isEmpty(requestName) ? apiOperation.value() : requestName + "_" + apiOperation.value();
remark = ObjectUtils.isEmpty(apiOperation.notes()) ? "" : apiOperation.notes();
} else {
requestName = ObjectUtils.isEmpty(requestName) ? handlerMethod.getMethod().getName() : requestName + "_" + handlerMethod.getMethod().getName();
}
map.put("requestName", requestName);
map.put("remark", remark);
return map;
}
private String getRequestType(RequestMethodsRequestCondition methodsCondition) {
String requestType = null;
int count = 0;
for (RequestMethod requestMethod : methodsCondition.getMethods()) {
requestType = requestMethod.toString();
count++;
}
return requestType == null || count > 1 ? "POST" : requestType;
}
private String getRequestUrl(String url) {
String separator = "/";
String separator2 = "//";
String separator3 = "///";
String contextPath = webApplicationContext.getEnvironment().getProperty("server.servlet.context-path");
String requestUrl;
if (!ObjectUtils.isEmpty(contextPath) && !separator.equals(contextPath)) {
requestUrl = contextPath + url;
if (requestUrl.contains(separator3) || requestUrl.contains(separator2)) {
requestUrl = requestUrl.replace(separator3, separator);
requestUrl = requestUrl.replace(separator2, separator);
}
} else {
requestUrl = url;
}
return requestUrl;
}
}