更新依赖,整理swagger2 3.0

This commit is contained in:
wanggeng 2022-05-12 16:05:42 +08:00
parent 38a28db163
commit 22034662f3
7 changed files with 127 additions and 149 deletions

View File

@ -54,15 +54,11 @@
<!-- spring end -->
<!-- swagger start -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>

View File

@ -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;
}
}

View File

@ -1,26 +1,20 @@
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 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.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
@ -32,174 +26,100 @@ import java.util.List;
**/
@Configuration
public class SwaggerConfig {
@Value("${swagger.title:相关接口文档}")
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();
}
@Autowired
private SwaggerProperties swaggerProperties;
@Bean
public Docket createRestApiSystem() {
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)
return new Docket(DocumentationType.OAS_30)
.enable(swaggerProperties.getActive())
.pathMapping("/")
.groupName(ISystemConstant.API_GROUP_SYSTEM)
.apiInfo(apiInfo())
.select()
.apis(Predicates.or(basePackages))
.paths(PathSelectors.ant(ISystemConstant.API_PREFIX + "/**"))
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.ant("/*" + ISystemConstant.API_PREFIX + "/**"))
.build();
}
@Bean
public Docket createRestApiResource() {
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)
return new Docket(DocumentationType.OAS_30)
.enable(swaggerProperties.getActive())
.pathMapping("/")
.groupName(ISystemConstant.API_GROUP_RESOURCE)
.apiInfo(apiInfo())
.select()
.apis(Predicates.or(basePackages))
.paths(PathSelectors.ant(ISystemConstant.RESOURCE_PREFIX + "/**"))
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.ant("/*" + ISystemConstant.RESOURCE_PREFIX + "/**"))
.build();
}
@Bean
public Docket createRestApiApp() {
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)
return new Docket(DocumentationType.OAS_30)
.enable(swaggerProperties.getActive())
.pathMapping("/")
.groupName(ISystemConstant.API_GROUP_APP)
.apiInfo(apiInfo())
.select()
.apis(Predicates.or(basePackages))
.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/**"))
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.ant("/*" + ISystemConstant.APP_PREFIX + "/**"))
.build();
}
@Bean
public Docket createRestApiWechat() {
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)
return new Docket(DocumentationType.OAS_30)
.enable(swaggerProperties.getActive())
.pathMapping("/")
.groupName(ISystemConstant.API_GROUP_WECHAT)
.apiInfo(apiInfo())
.select()
.apis(Predicates.or(basePackages))
.paths(PathSelectors.ant(ISystemConstant.WECHAT_PREFIX + "/**"))
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.ant("/*" + ISystemConstant.WECHAT_PREFIX + "/**"))
.build();
}
@Bean
public Docket createRestApiWechatMiniapp() {
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)
return new Docket(DocumentationType.OAS_30)
.enable(swaggerProperties.getActive())
.pathMapping("/")
.groupName(ISystemConstant.API_GROUP_WECHAT_MINI_APP)
.apiInfo(apiInfo())
.select()
.apis(Predicates.or(basePackages))
.paths(PathSelectors.ant(ISystemConstant.WECHAT_MINI_APP_PREFIX + "/**"))
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.ant("/*" + ISystemConstant.WECHAT_MINI_APP_PREFIX + "/**"))
.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() {
return new ApiInfoBuilder()
.title(title)
.description(description)
.termsOfServiceUrl(serviceUrl)
.version(version)
.title(swaggerProperties.getTitle())
.description(swaggerProperties.getDescription())
.contact(new Contact("WenG", null, "450292408@qq.com"))
.version(swaggerProperties.getVersion())
.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());
}
}

View File

@ -36,7 +36,7 @@
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.0.0.RELEASE</version>
<version>2.5.13</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>

View File

@ -46,7 +46,7 @@
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.0.0.RELEASE</version>
<version>2.5.13</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>

View File

@ -46,7 +46,7 @@ public class FileV2AppController extends DefaultBaseController {
@ApiOperation(value = "上传文件", notes = "上传文件接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "userId", paramType = "token"),
@ApiImplicitParam(name = "userId", value = "userId", paramType = "header"),
@ApiImplicitParam(name = "file", value = "文件name", paramType = "query")
})
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@ -70,7 +70,7 @@ public class FileV2AppController extends DefaultBaseController {
@ApiOperation(value = "上传图片", notes = "上传图片接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "userId", paramType = "token"),
@ApiImplicitParam(name = "userId", value = "userId", paramType = "header"),
@ApiImplicitParam(name = "image", value = "文件name", paramType = "query")
})
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})

View File

@ -69,8 +69,8 @@
<jspApi.version>2.0</jspApi.version>
<jstl.version>1.2</jstl.version>
<thumbnailator.version>0.4.8</thumbnailator.version>
<swagger.version>2.5.0</swagger.version>
<knife4j.version>2.0.9</knife4j.version>
<swagger.version>3.0.0</swagger.version>
<knife4j.version>3.0.3</knife4j.version>
<jodatime.version>2.9.4</jodatime.version>
<commons-text.version>1.6</commons-text.version>
<common-pool2.version>2.6.0</common-pool2.version>