From 78c32e06212ee8df77e1fc5835d74f75bd93933f Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Tue, 28 Apr 2020 12:06:33 +0800 Subject: [PATCH] =?UTF-8?q?swagger=E6=B7=BB=E5=8A=A0=E5=88=86=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/cm/common/config/SwaggerConfig.java | 89 ++++++++++++++++++- .../cm/common/constants/ISystemConstant.java | 21 +++++ 2 files changed, 108 insertions(+), 2 deletions(-) diff --git a/cloud-common/src/main/java/com/cm/common/config/SwaggerConfig.java b/cloud-common/src/main/java/com/cm/common/config/SwaggerConfig.java index c0c35c4..3d28c31 100644 --- a/cloud-common/src/main/java/com/cm/common/config/SwaggerConfig.java +++ b/cloud-common/src/main/java/com/cm/common/config/SwaggerConfig.java @@ -1,5 +1,6 @@ package com.cm.common.config; +import com.cm.common.constants.ISystemConstant; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -37,13 +38,17 @@ public class SwaggerConfig { @Value("${swagger.swagger-base-package}") private String swaggerBasePackage = "com.cm"; - @Bean - public Docket createRestApi() { + private List responseMessageList() { List 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 responseMessageList = responseMessageList(); return new Docket(DocumentationType.SWAGGER_2) .globalResponseMessage(RequestMethod.POST, responseMessageList) .globalResponseMessage(RequestMethod.DELETE, responseMessageList) @@ -56,6 +61,86 @@ public class SwaggerConfig { .build(); } + @Bean + public Docket createRestApiSystem() { + List 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_SYSTEM) + .apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage(swaggerBasePackage)) + .paths(PathSelectors.ant("/api/**")) + .build(); + } + + @Bean + public Docket createRestApiResource() { + List 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_RESOURCE) + .apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage(swaggerBasePackage)) + .paths(PathSelectors.ant("/resource/**")) + .build(); + } + + @Bean + public Docket createRestApiApp() { + List 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_APP) + .apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage(swaggerBasePackage)) + .paths(PathSelectors.ant("/app/**")) + .build(); + } + + @Bean + public Docket createRestApiRoute() { + List 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(RequestHandlerSelectors.basePackage(swaggerBasePackage)) + .paths(PathSelectors.ant("/route/**")) + .build(); + } + + @Bean + public Docket createRestApiWechat() { + List 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_WECHAT) + .apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage(swaggerBasePackage)) + .paths(PathSelectors.ant("/wechat/**")) + .build(); + } + private ApiInfo apiInfo() { return new ApiInfoBuilder() .title(swaggerTitle) diff --git a/cloud-common/src/main/java/com/cm/common/constants/ISystemConstant.java b/cloud-common/src/main/java/com/cm/common/constants/ISystemConstant.java index 8d14580..43da154 100644 --- a/cloud-common/src/main/java/com/cm/common/constants/ISystemConstant.java +++ b/cloud-common/src/main/java/com/cm/common/constants/ISystemConstant.java @@ -32,6 +32,26 @@ public interface ISystemConstant { * 微信接口前缀 */ String API_TAGS_WECHAT_PREFIX = "微信接口-"; + /** + * 系统接口分组 + */ + String API_GROUP_SYSTEM = "SYSTEM"; + /** + * 资源接口分组 + */ + String API_GROUP_RESOURCE = "RESOURCE"; + /** + * APP接口分组 + */ + String API_GROUP_APP = "APP"; + /** + * 路由接口分组 + */ + String API_GROUP_ROUTE = "ROUTE"; + /** + * 微信接口分组 + */ + String API_GROUP_WECHAT = "WECHAT"; /** * api前缀 */ @@ -140,4 +160,5 @@ public interface ISystemConstant { * session的AccessToken */ String SESSION_WECHAT_ACCESS_TOKEN = "SESSION_WECHAT_ACCESS_TOKEN"; + }