|
@@ -3,21 +3,39 @@ package com.persagy.apm.common.config;
|
|
|
import com.fasterxml.classmate.ResolvedType;
|
|
|
import com.google.common.base.Optional;
|
|
|
import com.persagy.apm.common.model.annotation.SwaggerDisplayEnum;
|
|
|
+import com.persagy.common.enums.ResponseCode;
|
|
|
import io.swagger.annotations.ApiModelProperty;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.assertj.core.util.Lists;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
import org.springframework.core.annotation.AnnotationUtils;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.ReflectionUtils;
|
|
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
|
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
|
|
import springfox.documentation.builders.ModelPropertyBuilder;
|
|
|
+import springfox.documentation.builders.PathSelectors;
|
|
|
+import springfox.documentation.builders.RequestHandlerSelectors;
|
|
|
+import springfox.documentation.builders.ResponseMessageBuilder;
|
|
|
import springfox.documentation.schema.Annotations;
|
|
|
+import springfox.documentation.schema.ModelRef;
|
|
|
+import springfox.documentation.service.ApiInfo;
|
|
|
+import springfox.documentation.service.Contact;
|
|
|
+import springfox.documentation.service.ResponseMessage;
|
|
|
import springfox.documentation.spi.DocumentationType;
|
|
|
import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
|
|
|
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;
|
|
|
+import springfox.documentation.spring.web.plugins.Docket;
|
|
|
import springfox.documentation.swagger.schema.ApiModelProperties;
|
|
|
+import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
@@ -32,8 +50,50 @@ import java.util.stream.Collectors;
|
|
|
@Component
|
|
|
@Primary
|
|
|
@Slf4j
|
|
|
-public class SwaggerDisplayConfig implements ModelPropertyBuilderPlugin {
|
|
|
+@EnableSwagger2
|
|
|
+public class SwaggerDisplayConfig extends WebMvcConfigurationSupport implements ModelPropertyBuilderPlugin {
|
|
|
+ @Value("${swagger.title:接口文档}")
|
|
|
+ private String title;
|
|
|
+ @Value("${swagger.desc:--}")
|
|
|
+ private String desc;
|
|
|
+ @Value("${swagger.version:v1.0.0}")
|
|
|
+ private String version;
|
|
|
+ @Value("${swagger.author:persagy}")
|
|
|
+ private String author;
|
|
|
+ @Value("${swagger.serviceUrl:www.persagy.com}")
|
|
|
+ private String serviceUrl;
|
|
|
+ @Value("${swagger.groupName:api}")
|
|
|
+ private String groupName;
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public Docket docket() {
|
|
|
+ List<ResponseMessage> responseMessageList = Lists.newArrayList();
|
|
|
+ return new Docket(DocumentationType.SWAGGER_2)
|
|
|
+ .globalResponseMessage(RequestMethod.GET, responseMessageList)
|
|
|
+ .globalResponseMessage(RequestMethod.POST, responseMessageList)
|
|
|
+ .globalResponseMessage(RequestMethod.PUT, responseMessageList)
|
|
|
+ .globalResponseMessage(RequestMethod.DELETE, responseMessageList)
|
|
|
+ .apiInfo(apiInfo())
|
|
|
+ .select()
|
|
|
+ .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
|
|
|
+ .paths(PathSelectors.any())
|
|
|
+ .build().groupName(groupName);
|
|
|
+ }
|
|
|
|
|
|
+ private ApiInfo apiInfo() {
|
|
|
+ //作者信息
|
|
|
+ Contact contact = new Contact(author, "www.persagy.com", "");
|
|
|
+ return new ApiInfo(
|
|
|
+ title,
|
|
|
+ desc,
|
|
|
+ version,
|
|
|
+ serviceUrl,
|
|
|
+ contact,
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ new ArrayList<>()
|
|
|
+ );
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public void apply(ModelPropertyContext context) {
|
|
@@ -128,4 +188,11 @@ public class SwaggerDisplayConfig implements ModelPropertyBuilderPlugin {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ protected void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
|
+ registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
|
|
|
+ registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
|
|
|
+ registry.addResourceHandler("/img/**").addResourceLocations("classpath:/META-INF/resources/");
|
|
|
+ registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
|
|
|
+ }
|
|
|
}
|