更新依赖,整理swagger2 3.0
This commit is contained in:
parent
38a28db163
commit
22034662f3
@ -54,15 +54,11 @@
|
|||||||
<!-- spring end -->
|
<!-- spring end -->
|
||||||
|
|
||||||
<!-- swagger start -->
|
<!-- swagger start -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.springfox</groupId>
|
<groupId>io.springfox</groupId>
|
||||||
<artifactId>springfox-swagger2</artifactId>
|
<artifactId>springfox-boot-starter</artifactId>
|
||||||
<exclusions>
|
<version>3.0.0</version>
|
||||||
<exclusion>
|
|
||||||
<groupId>org.slf4j</groupId>
|
|
||||||
<artifactId>slf4j-api</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.springfox</groupId>
|
<groupId>io.springfox</groupId>
|
||||||
|
@ -0,0 +1,62 @@
|
|||||||
|
package ink.wgink.properties.swagger;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName: SwaggerProperties
|
||||||
|
* @Description: swagger配置
|
||||||
|
* @Author: wanggeng
|
||||||
|
* @Date: 2022/5/12 14:21
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix = "swagger")
|
||||||
|
public class SwaggerProperties {
|
||||||
|
|
||||||
|
private Boolean active;
|
||||||
|
private String title;
|
||||||
|
private String description;
|
||||||
|
private String host;
|
||||||
|
private String version;
|
||||||
|
|
||||||
|
public Boolean getActive() {
|
||||||
|
return active == null ? true : active;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setActive(Boolean active) {
|
||||||
|
this.active = active;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title == null ? "相关接口文档" : title.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description == null ? "相关接口文档" : description.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHost() {
|
||||||
|
return host == null ? "http://www.wgink.ink" : host.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHost(String host) {
|
||||||
|
this.host = host;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getVersion() {
|
||||||
|
return version == null ? "" : version.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVersion(String version) {
|
||||||
|
this.version = version;
|
||||||
|
}
|
||||||
|
}
|
@ -1,26 +1,20 @@
|
|||||||
package ink.wgink.common.config;
|
package ink.wgink.common.config;
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
|
||||||
import com.google.common.base.Optional;
|
|
||||||
import com.google.common.base.Predicate;
|
|
||||||
import com.google.common.base.Predicates;
|
|
||||||
import ink.wgink.interfaces.consts.ISystemConstant;
|
import ink.wgink.interfaces.consts.ISystemConstant;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import ink.wgink.properties.swagger.SwaggerProperties;
|
||||||
|
import io.swagger.models.auth.In;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import springfox.documentation.RequestHandler;
|
|
||||||
import springfox.documentation.builders.ApiInfoBuilder;
|
import springfox.documentation.builders.ApiInfoBuilder;
|
||||||
import springfox.documentation.builders.PathSelectors;
|
import springfox.documentation.builders.PathSelectors;
|
||||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||||
import springfox.documentation.builders.ResponseMessageBuilder;
|
import springfox.documentation.service.*;
|
||||||
import springfox.documentation.service.ApiInfo;
|
|
||||||
import springfox.documentation.service.ResponseMessage;
|
|
||||||
import springfox.documentation.spi.DocumentationType;
|
import springfox.documentation.spi.DocumentationType;
|
||||||
|
import springfox.documentation.spi.service.contexts.SecurityContext;
|
||||||
import springfox.documentation.spring.web.plugins.Docket;
|
import springfox.documentation.spring.web.plugins.Docket;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import java.util.Collections;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -32,174 +26,100 @@ import java.util.List;
|
|||||||
**/
|
**/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class SwaggerConfig {
|
public class SwaggerConfig {
|
||||||
|
@Autowired
|
||||||
@Value("${swagger.title:相关接口文档}")
|
private SwaggerProperties swaggerProperties;
|
||||||
private String title;
|
|
||||||
@Value("${swagger.description:相关接口文档}")
|
|
||||||
private String description;
|
|
||||||
@Value("${swagger.service-url:http://www.wgink.ink}")
|
|
||||||
private String serviceUrl;
|
|
||||||
@Value("${swagger.version:1.0}")
|
|
||||||
private String version;
|
|
||||||
@Value("#{'${swagger.base-package-list}'.split(',')}")
|
|
||||||
private List<String> basePackageList;
|
|
||||||
private Predicate<RequestHandler>[] basePackages;
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
private void initBasePackages() {
|
|
||||||
basePackages = new Predicate[basePackageList.size()];
|
|
||||||
for (int i = 0; i < basePackages.length; i++) {
|
|
||||||
basePackages[i] = RequestHandlerSelectors.basePackage(basePackageList.get(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<ResponseMessage> responseMessageList() {
|
|
||||||
List<ResponseMessage> responseMessageList = new ArrayList<>();
|
|
||||||
responseMessageList.add(new ResponseMessageBuilder().code(400).message("请求失败").build());
|
|
||||||
responseMessageList.add(new ResponseMessageBuilder().code(404).message("请求不存在").build());
|
|
||||||
responseMessageList.add(new ResponseMessageBuilder().code(503).message("服务不可用").build());
|
|
||||||
return responseMessageList;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public Docket createRestGlobalApi() {
|
|
||||||
List<ResponseMessage> responseMessageList = responseMessageList();
|
|
||||||
return new Docket(DocumentationType.SWAGGER_2)
|
|
||||||
.globalResponseMessage(RequestMethod.POST, responseMessageList)
|
|
||||||
.globalResponseMessage(RequestMethod.DELETE, responseMessageList)
|
|
||||||
.globalResponseMessage(RequestMethod.PUT, responseMessageList)
|
|
||||||
.globalResponseMessage(RequestMethod.GET, responseMessageList)
|
|
||||||
.apiInfo(apiInfo())
|
|
||||||
.select()
|
|
||||||
.apis(Predicates.or(basePackages))
|
|
||||||
.paths(PathSelectors.any())
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Docket createRestApiSystem() {
|
public Docket createRestApiSystem() {
|
||||||
List<ResponseMessage> responseMessageList = responseMessageList();
|
return new Docket(DocumentationType.OAS_30)
|
||||||
return new Docket(DocumentationType.SWAGGER_2)
|
.enable(swaggerProperties.getActive())
|
||||||
.globalResponseMessage(RequestMethod.POST, responseMessageList)
|
.pathMapping("/")
|
||||||
.globalResponseMessage(RequestMethod.DELETE, responseMessageList)
|
|
||||||
.globalResponseMessage(RequestMethod.PUT, responseMessageList)
|
|
||||||
.globalResponseMessage(RequestMethod.GET, responseMessageList)
|
|
||||||
.groupName(ISystemConstant.API_GROUP_SYSTEM)
|
.groupName(ISystemConstant.API_GROUP_SYSTEM)
|
||||||
.apiInfo(apiInfo())
|
.apiInfo(apiInfo())
|
||||||
.select()
|
.select()
|
||||||
.apis(Predicates.or(basePackages))
|
.apis(RequestHandlerSelectors.any())
|
||||||
.paths(PathSelectors.ant(ISystemConstant.API_PREFIX + "/**"))
|
.paths(PathSelectors.ant("/*" + ISystemConstant.API_PREFIX + "/**"))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Docket createRestApiResource() {
|
public Docket createRestApiResource() {
|
||||||
List<ResponseMessage> responseMessageList = responseMessageList();
|
return new Docket(DocumentationType.OAS_30)
|
||||||
return new Docket(DocumentationType.SWAGGER_2)
|
.enable(swaggerProperties.getActive())
|
||||||
.globalResponseMessage(RequestMethod.POST, responseMessageList)
|
.pathMapping("/")
|
||||||
.globalResponseMessage(RequestMethod.DELETE, responseMessageList)
|
|
||||||
.globalResponseMessage(RequestMethod.PUT, responseMessageList)
|
|
||||||
.globalResponseMessage(RequestMethod.GET, responseMessageList)
|
|
||||||
.groupName(ISystemConstant.API_GROUP_RESOURCE)
|
.groupName(ISystemConstant.API_GROUP_RESOURCE)
|
||||||
.apiInfo(apiInfo())
|
.apiInfo(apiInfo())
|
||||||
.select()
|
.select()
|
||||||
.apis(Predicates.or(basePackages))
|
.apis(RequestHandlerSelectors.any())
|
||||||
.paths(PathSelectors.ant(ISystemConstant.RESOURCE_PREFIX + "/**"))
|
.paths(PathSelectors.ant("/*" + ISystemConstant.RESOURCE_PREFIX + "/**"))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Docket createRestApiApp() {
|
public Docket createRestApiApp() {
|
||||||
List<ResponseMessage> responseMessageList = responseMessageList();
|
return new Docket(DocumentationType.OAS_30)
|
||||||
return new Docket(DocumentationType.SWAGGER_2)
|
.enable(swaggerProperties.getActive())
|
||||||
.globalResponseMessage(RequestMethod.POST, responseMessageList)
|
.pathMapping("/")
|
||||||
.globalResponseMessage(RequestMethod.DELETE, responseMessageList)
|
|
||||||
.globalResponseMessage(RequestMethod.PUT, responseMessageList)
|
|
||||||
.globalResponseMessage(RequestMethod.GET, responseMessageList)
|
|
||||||
.groupName(ISystemConstant.API_GROUP_APP)
|
.groupName(ISystemConstant.API_GROUP_APP)
|
||||||
.apiInfo(apiInfo())
|
.apiInfo(apiInfo())
|
||||||
.select()
|
.select()
|
||||||
.apis(Predicates.or(basePackages))
|
.apis(RequestHandlerSelectors.any())
|
||||||
.paths(PathSelectors.ant(ISystemConstant.APP_PREFIX + "/**"))
|
.paths(PathSelectors.ant("/*" + ISystemConstant.APP_PREFIX + "/**"))
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public Docket createRestApiRoute() {
|
|
||||||
List<ResponseMessage> responseMessageList = responseMessageList();
|
|
||||||
return new Docket(DocumentationType.SWAGGER_2)
|
|
||||||
.globalResponseMessage(RequestMethod.POST, responseMessageList)
|
|
||||||
.globalResponseMessage(RequestMethod.DELETE, responseMessageList)
|
|
||||||
.globalResponseMessage(RequestMethod.PUT, responseMessageList)
|
|
||||||
.globalResponseMessage(RequestMethod.GET, responseMessageList)
|
|
||||||
.groupName(ISystemConstant.API_GROUP_ROUTE)
|
|
||||||
.apiInfo(apiInfo())
|
|
||||||
.select()
|
|
||||||
.apis(Predicates.or(basePackages))
|
|
||||||
.paths(PathSelectors.ant("/route/**"))
|
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Docket createRestApiWechat() {
|
public Docket createRestApiWechat() {
|
||||||
List<ResponseMessage> responseMessageList = responseMessageList();
|
return new Docket(DocumentationType.OAS_30)
|
||||||
return new Docket(DocumentationType.SWAGGER_2)
|
.enable(swaggerProperties.getActive())
|
||||||
.globalResponseMessage(RequestMethod.POST, responseMessageList)
|
.pathMapping("/")
|
||||||
.globalResponseMessage(RequestMethod.DELETE, responseMessageList)
|
|
||||||
.globalResponseMessage(RequestMethod.PUT, responseMessageList)
|
|
||||||
.globalResponseMessage(RequestMethod.GET, responseMessageList)
|
|
||||||
.groupName(ISystemConstant.API_GROUP_WECHAT)
|
.groupName(ISystemConstant.API_GROUP_WECHAT)
|
||||||
.apiInfo(apiInfo())
|
.apiInfo(apiInfo())
|
||||||
.select()
|
.select()
|
||||||
.apis(Predicates.or(basePackages))
|
.apis(RequestHandlerSelectors.any())
|
||||||
.paths(PathSelectors.ant(ISystemConstant.WECHAT_PREFIX + "/**"))
|
.paths(PathSelectors.ant("/*" + ISystemConstant.WECHAT_PREFIX + "/**"))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Docket createRestApiWechatMiniapp() {
|
public Docket createRestApiWechatMiniapp() {
|
||||||
List<ResponseMessage> responseMessageList = responseMessageList();
|
return new Docket(DocumentationType.OAS_30)
|
||||||
return new Docket(DocumentationType.SWAGGER_2)
|
.enable(swaggerProperties.getActive())
|
||||||
.globalResponseMessage(RequestMethod.POST, responseMessageList)
|
.pathMapping("/")
|
||||||
.globalResponseMessage(RequestMethod.DELETE, responseMessageList)
|
|
||||||
.globalResponseMessage(RequestMethod.PUT, responseMessageList)
|
|
||||||
.globalResponseMessage(RequestMethod.GET, responseMessageList)
|
|
||||||
.groupName(ISystemConstant.API_GROUP_WECHAT_MINI_APP)
|
.groupName(ISystemConstant.API_GROUP_WECHAT_MINI_APP)
|
||||||
.apiInfo(apiInfo())
|
.apiInfo(apiInfo())
|
||||||
.select()
|
.select()
|
||||||
.apis(Predicates.or(basePackages))
|
.apis(RequestHandlerSelectors.any())
|
||||||
.paths(PathSelectors.ant(ISystemConstant.WECHAT_MINI_APP_PREFIX + "/**"))
|
.paths(PathSelectors.ant("/*" + ISystemConstant.WECHAT_MINI_APP_PREFIX + "/**"))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置授权信息
|
||||||
|
*/
|
||||||
|
private List<SecurityScheme> securitySchemes() {
|
||||||
|
ApiKey apiKey = new ApiKey("BASE_TOKEN", "token", In.HEADER.toValue());
|
||||||
|
return Collections.singletonList(apiKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 授权信息全局应用
|
||||||
|
*/
|
||||||
|
private List<SecurityContext> securityContexts() {
|
||||||
|
return Collections.singletonList(
|
||||||
|
SecurityContext.builder()
|
||||||
|
.securityReferences(Collections.singletonList(new SecurityReference("BASE_TOKEN", new AuthorizationScope[]{new AuthorizationScope("global", "")})))
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
private ApiInfo apiInfo() {
|
private ApiInfo apiInfo() {
|
||||||
return new ApiInfoBuilder()
|
return new ApiInfoBuilder()
|
||||||
.title(title)
|
.title(swaggerProperties.getTitle())
|
||||||
.description(description)
|
.description(swaggerProperties.getDescription())
|
||||||
.termsOfServiceUrl(serviceUrl)
|
.contact(new Contact("WenG", null, "450292408@qq.com"))
|
||||||
.version(version)
|
.version(swaggerProperties.getVersion())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Predicate<RequestHandler> basePackage(final List<String> basePackageList) {
|
|
||||||
return input -> declaringClass(input).transform(handlerPackage(basePackageList)).or(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Function<Class<?>, Boolean> handlerPackage(final List<String> basePackageList) {
|
|
||||||
return input -> {
|
|
||||||
// 循环判断匹配
|
|
||||||
for (String strPackage : basePackageList) {
|
|
||||||
boolean isMatch = input.getPackage().getName().startsWith(strPackage);
|
|
||||||
if (isMatch) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Optional<? extends Class<?>> declaringClass(RequestHandler input) {
|
|
||||||
return Optional.fromNullable(input.getClass());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.security.oauth.boot</groupId>
|
<groupId>org.springframework.security.oauth.boot</groupId>
|
||||||
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
|
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
|
||||||
<version>2.0.0.RELEASE</version>
|
<version>2.5.13</version>
|
||||||
<exclusions>
|
<exclusions>
|
||||||
<exclusion>
|
<exclusion>
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
@ -46,7 +46,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.security.oauth.boot</groupId>
|
<groupId>org.springframework.security.oauth.boot</groupId>
|
||||||
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
|
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
|
||||||
<version>2.0.0.RELEASE</version>
|
<version>2.5.13</version>
|
||||||
<exclusions>
|
<exclusions>
|
||||||
<exclusion>
|
<exclusion>
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
@ -46,7 +46,7 @@ public class FileV2AppController extends DefaultBaseController {
|
|||||||
|
|
||||||
@ApiOperation(value = "上传文件", notes = "上传文件接口")
|
@ApiOperation(value = "上传文件", notes = "上传文件接口")
|
||||||
@ApiImplicitParams({
|
@ApiImplicitParams({
|
||||||
@ApiImplicitParam(name = "userId", value = "userId", paramType = "token"),
|
@ApiImplicitParam(name = "userId", value = "userId", paramType = "header"),
|
||||||
@ApiImplicitParam(name = "file", value = "文件name", paramType = "query")
|
@ApiImplicitParam(name = "file", value = "文件name", paramType = "query")
|
||||||
})
|
})
|
||||||
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
|
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
|
||||||
@ -70,7 +70,7 @@ public class FileV2AppController extends DefaultBaseController {
|
|||||||
|
|
||||||
@ApiOperation(value = "上传图片", notes = "上传图片接口")
|
@ApiOperation(value = "上传图片", notes = "上传图片接口")
|
||||||
@ApiImplicitParams({
|
@ApiImplicitParams({
|
||||||
@ApiImplicitParam(name = "userId", value = "userId", paramType = "token"),
|
@ApiImplicitParam(name = "userId", value = "userId", paramType = "header"),
|
||||||
@ApiImplicitParam(name = "image", value = "文件name", paramType = "query")
|
@ApiImplicitParam(name = "image", value = "文件name", paramType = "query")
|
||||||
})
|
})
|
||||||
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
|
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
|
||||||
|
4
pom.xml
4
pom.xml
@ -69,8 +69,8 @@
|
|||||||
<jspApi.version>2.0</jspApi.version>
|
<jspApi.version>2.0</jspApi.version>
|
||||||
<jstl.version>1.2</jstl.version>
|
<jstl.version>1.2</jstl.version>
|
||||||
<thumbnailator.version>0.4.8</thumbnailator.version>
|
<thumbnailator.version>0.4.8</thumbnailator.version>
|
||||||
<swagger.version>2.5.0</swagger.version>
|
<swagger.version>3.0.0</swagger.version>
|
||||||
<knife4j.version>2.0.9</knife4j.version>
|
<knife4j.version>3.0.3</knife4j.version>
|
||||||
<jodatime.version>2.9.4</jodatime.version>
|
<jodatime.version>2.9.4</jodatime.version>
|
||||||
<commons-text.version>1.6</commons-text.version>
|
<commons-text.version>1.6</commons-text.version>
|
||||||
<common-pool2.version>2.6.0</common-pool2.version>
|
<common-pool2.version>2.6.0</common-pool2.version>
|
||||||
|
Loading…
Reference in New Issue
Block a user