例如将 controller 分成四类,分别放到四个包下:
xxx.xxx.xxx.controller.manage
xxx.xxx.xxx.controller.client
xxx.xxx.xxx.controller.authority
xxx.xxx.xxx.controller.common
SwaggerConfig.java:
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;import java.util.ArrayList;
import java.util.Collections;
import java.util.List;@RefreshScope
@Configuration
@EnableSwagger2
public class SwaggerConfig {@Value("${system.swagger_show}")private boolean swaggerShow;@Beanpublic Docket manageGroup() {return this.buildDocket( "管理端接口","xxx.xxx.xxx.controller.manage" );}@Beanpublic Docket clientGroup() {return this.buildDocket( "移动端接口","xxx.xxx.xxx.controller.client" );}@Beanpublic Docket authorityGroup() {return this.buildDocket( "权限相关接口","xxx.xxx.xxx.controller.authority" );}@Beanpublic Docket commonGroup() {return this.buildDocket( "数据字典、行政区域公共接口","xxx.xxx.xxx.controller.common" );}private Docket buildDocket( String groupName,String basePackage ){return new Docket(DocumentationType.SWAGGER_2).groupName( groupName ).enable(this.swaggerShow).produces(Collections.singleton(MediaType.APPLICATION_JSON_VALUE)).genericModelSubstitutes(ResponseEntity.class).forCodeGeneration(false).useDefaultResponseMessages(false).pathMapping("/").select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).apis(RequestHandlerSelectors.basePackage( basePackage )).paths(PathSelectors.any()).build().globalOperationParameters( setHeaderToken() ).directModelSubstitute(java.sql.Timestamp.class, java.sql.Date.class).apiInfo(apiInfo());}private List<Parameter> setHeaderToken() {ParameterBuilder tokenPar = new ParameterBuilder();List<Parameter> pars = new ArrayList<>();tokenPar.name("witToken").description("登录token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();pars.add(tokenPar.build());return pars;}private ApiInfo apiInfo() {ApiInfo apiInfo = new ApiInfoBuilder().title("xxx系统( xxx) API").description("xxx系统( xxx) API").license(null).licenseUrl(null).contact(new Contact("developer", "", "developer@xxx.com")).version("1.0").build();return apiInfo;}}
效果: