---
title: "swagger2的分组配置"
author: "tllyf"
date: "2018-10-19T21:02:18Z"
tags: []
categories: ["编程语言"]
slug: "swagger2"
draft: false
lastmod: "2018-10-19T21:02:18Z"
id: 2432452
url: "https://tllyf.iteye.com/blog/2432452"
---

当微服务的服务越来越多swagger2展示的越来越长这时候需要进行分页展示
swagger2头上有下拉框可以groupName进行分组展示

java代码代码：

@Configuration
@EnableSwagger2
public class SwaggerConfig {

	 	@Bean
	    public Docket createRestApi() {
	 		
	 		
	        return new Docket(DocumentationType.SWAGGER_2)
	        		.globalOperationParameters(operationParameters())
	        		.groupName("第一个接口服务页面")
	        		.genericModelSubstitutes(DeferredResult.class)
	                .apiInfo(apiInfo())
	                .select()
	                .apis(RequestHandlerSelectors.basePackage("com.tan.oauth2.controller"))
	                .paths(PathSelectors.any())
	                .build();
	    }
	    private ApiInfo apiInfo() {
	        return new ApiInfoBuilder()
	                .title(" createRestApi  Spring Boot中使用Swagger2构建RESTful APIs")
	                .description("更多Spring Boot相关文章请关注：http://blog.didispace.com/")
	                .termsOfServiceUrl("http://github.com/")
	                .contact("程序猿痛苦")
	                .version("1.0")
	                .build();
	    }
	    @Bean
	    public Docket openRestApi() {
	    	Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() {
	            @Override
	            public boolean apply(RequestHandler input) {
//	                Class<?> declaringClass = input.declaringClass();
//	                if (declaringClass == BasicErrorController.class)// 排除
//	                    return false;
//	                if(declaringClass.isAnnotationPresent(ApiOperation.class)) // 被注解的类
//	                    return true;
//	                if(input.isAnnotatedWith(ResponseBody.class)) // 被注解的方法
//	                    return true;
//	                if (input.isAnnotatedWith(ApiOperation.class))//只有添加了ApiOperation注解的method才在API中显示
//	                    return true;
	                if (input.isAnnotatedWith(ApiOperation.class))//只有添加了ApiOperation注解的method才在API中显示
	                	return true;
	                return false;
	            }
	        };
	    	return new Docket(DocumentationType.SWAGGER_2)
	    			.groupName("第二个接口服务页面")
	    			.genericModelSubstitutes(DeferredResult.class)
	    			.apiInfo(openapiInfo())
	    			.select()
	    			.apis(RequestHandlerSelectors.basePackage("com.tan.oauth2.controller2"))
//	    			.apis(predicate)
	    			.paths(PathSelectors.any())
	    			.build();
	    }
	    private ApiInfo openapiInfo() {
	    	return new ApiInfoBuilder()
	    			.title("openRestApi Spring Boot中使用Swagger2构建RESTful APIs")
	    			.description("更多Spring Boot相关文章请关注：http://blog.didispace.com/")
	    			.termsOfServiceUrl("http://github.com/")
	    			.contact("程序猿痛苦")
	    			.version("1.0")
	    			.build();
	    }
	    
	    public  List<Parameter> operationParameters() {
	    	//添加head参数start
	    	ParameterBuilder tokenPar = new ParameterBuilder();
	    	List<Parameter> pars = new ArrayList<Parameter>();
	    	tokenPar.name("x-access-token")
	    	.description("令牌")
	    	.modelRef(new ModelRef("string"))
	    	.parameterType("header")
	    	.required(false)
	    	.build();
	    	pars.add(tokenPar.build());
	    	//添加head参数end
	    	return pars;  
	    }
	    
}

pom.xml文件配置

                 <dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
		</dependency>

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
		</dependency>