cm-cloud/cloud-common/src/main/java/com/cm/common/config/SwaggerConfig.java

170 lines
7.6 KiB
Java

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;
import org.springframework.web.bind.annotation.RequestMethod;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName: SwaggerConfig
* @Description: swagger的配置文件
* @Author: WangGeng
* @Date: 2019/3/1 3:58 PM
* @Version: 1.0
**/
@Configuration
public class SwaggerConfig {
@Value("${swagger.title}")
private String swaggerTitle = "相关接口文档";
@Value("${swagger.description}")
private String swaggerDescription = "相关接口文档";
@Value("${swagger.service-url}")
private String swaggerServiceUrl = "http://http://www.sucstep.com/";
@Value("${swagger.version}")
private String swaggerVersion = "1.0";
@Value("${swagger.swagger-base-package}")
private String swaggerBasePackage = "com.cm";
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(RequestHandlerSelectors.basePackage(swaggerBasePackage))
.paths(PathSelectors.any())
.build();
}
@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)
.groupName(ISystemConstant.API_GROUP_SYSTEM)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(swaggerBasePackage))
.paths(PathSelectors.ant("/api/**"))
.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)
.groupName(ISystemConstant.API_GROUP_RESOURCE)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(swaggerBasePackage))
.paths(PathSelectors.ant("/resource/**"))
.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)
.groupName(ISystemConstant.API_GROUP_APP)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(swaggerBasePackage))
.paths(PathSelectors.ant("/app/**"))
.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(RequestHandlerSelectors.basePackage(swaggerBasePackage))
.paths(PathSelectors.ant("/route/**"))
.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)
.groupName(ISystemConstant.API_GROUP_WECHAT)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(swaggerBasePackage))
.paths(PathSelectors.ant("/wechat/**"))
.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)
.groupName(ISystemConstant.API_GROUP_WECHAT_MINI_APP)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(swaggerBasePackage))
.paths(PathSelectors.ant("/wxminiapp/**"))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(swaggerTitle)
.description(swaggerDescription)
.termsOfServiceUrl(swaggerServiceUrl)
.version(swaggerVersion)
.build();
}
}