swagger2 分组

https://blog.csdn.net/stonexmx/article/details/77604571

  1. 自定义注解
 package com.awj.mall.restfulapi.common.swagger.annotion;


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ApiVersion {

     String[] group();

}
  1. 创建版本常量类
package com.awj.mall.restfulapi.common.swagger.consts;

public interface ApiVersionConsts {

    String V_2_2_1 = "v_2_2_1";




}

3.改造swagger2生成文档类

package com.awj.mall.restfulapi.config;

import java.util.Arrays;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;

import com.awj.mall.restfulapi.common.swagger.annotion.ApiVersion;
import com.awj.mall.restfulapi.common.swagger.consts.ApiVersionConsts;
import com.google.common.base.Predicate;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class Swagger2 {
	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.select()
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				.paths(PathSelectors.any())
				.build();
	}
	
//	@Bean
//	public Docket v2_2_1(){
//		return new Docket(DocumentationType.SWAGGER_2)
//				.apiInfo(apiInfo())
//				.groupName(ApiVersionConsts.V_2_2_1)
//				.select()
//				.apis(input -> {
//					ApiVersion apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersion.class);
//					if(apiVersion!=null&& Arrays.asList(apiVersion.group()).contains(ApiVersionConsts.V_2_2_1)){
//						return true;
//					}
//					return false;
//				})//controller路径
//				.paths(PathSelectors.any())
//				.build();
//	}
	
	
	@Bean
	public Docket v2_2_1(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(ApiVersionConsts.V_2_2_1)
                .genericModelSubstitutes(DeferredResult.class)
//              .genericModelSubstitutes(ResponseEntity.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(false)
                .select()
                .apis(new Predicate() {
                    @Override
                    public boolean apply(RequestHandler input) {
                    	ApiVersion apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersion.class);
        				if(apiVersion!=null&& Arrays.asList(apiVersion.group()).contains(ApiVersionConsts.V_2_2_1)){
        					return true;
        				}
        				return false;
                    }
                })
                .paths(PathSelectors.any())//过滤的接口
                .build()
                .apiInfo(apiInfo());

	}
	
	
	/* @Bean
	    public Docket packageApi() {
		 return new Docket(DocumentationType.SWAGGER_2)
				 .groupName("app")
				 .apiInfo(apiInfo())
				 .select()
				 .apis(RequestHandlerSelectors.basePackage("com.awj.mall.restfulapi.modular"))
				 .paths(PathSelectors.any())
				 .build();
	    }*/
	
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("安万家-数据接口API")
				.version("v1")
				.build();
	}
}

4.利用注解进行swagger2分组

/**
 * 刷新个人信息(app端登入之后使用)
 * 
 * @param requestApiParam
 * @return
 */
@RequestMapping(value = "v1/getuserbaseinfo", method = RequestMethod.POST)
@ApiOperation(value = "刷新个人信息(app端登入之后使用)", notes = "一期一阶")
@ApiVersion(group= {ApiVersionConsts.V_2_2_1})
public ResultEntity getUserBaseInfo(@RequestBody RequestApiParam requestApiParam) {
	assertUidNotNull(requestApiParam);
	UserBaseInfoVO userBaseInfoVO = new UserBaseInfoVO();
	UserBaseInfo userBaserInfo = this.userBaseInfoService.selectByUserId(HttpConsts.NO, HttpConsts.YES,
			requestApiParam.getUid());
	if (userBaserInfo == null) {
		return error(ResultCode.USER_NOT_EXIST);
	}
	BeanUtil.copyProperties(userBaserInfo, userBaseInfoVO);
	final String randomKey = jwtTokenUtil.getRandomKey();
	String jwtToken = jwtTokenUtil.generateToken(userBaseInfoVO.getId(), randomKey);
	// 设置jwtToken
	userBaseInfoVO.setRandomKey(randomKey);
	userBaseInfoVO.setAuthToken(jwtToken);
	userBaseInfoVO.setUserRole(UserUtil.getUserRoleByUserId(requestApiParam.getUid()));
	UserFirstpartyInfo userFirstpartyInfo = new UserFirstpartyInfo();
	if (UserConsts.SHOP_SERVICE.equals(userBaserInfo.getIdentity())) {
		List userFirstpartyInfos = this.iUserFirstpartyInfoService
				.selectList(new EntityWrapper().eq("is_deleted", HttpConsts.NO)
						.eq("is_enable", HttpConsts.YES).eq("user_id", requestApiParam.getUid())
						.orderBy("created", false));
		if (userFirstpartyInfos != null && !userFirstpartyInfos.isEmpty()) {
			userFirstpartyInfo = userFirstpartyInfos.get(0);
		}
	}
	userBaseInfoVO.setUserFirstpartyInfo(userFirstpartyInfo);
	if (userBaserInfo.getpServiceUserId() != null) {
		UserBaseInfo pUserBaserInfo = this.userBaseInfoService.selectByUserId(HttpConsts.NO, HttpConsts.YES,
				userBaserInfo.getpServiceUserId());
		userBaseInfoVO.setpUserBaseInfo(pUserBaserInfo == null ? new UserBaseInfo() : pUserBaserInfo);
	}
	return success(userBaseInfoVO);
}

你可能感兴趣的:(swagger2 分组)