spring boot swagger 分组 定制 显示API

pom.xml

   

	
		 
        
            io.springfox
            springfox-swagger2
            2.6.1
        

        
            io.springfox
            springfox-swagger-ui
            2.6.1
        


configuration

import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;

import org.springframework.boot.autoconfigure.web.BasicErrorController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * SwaggerConfig
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {


    /**
     * 定义api组,
     */
    @Bean
    public Docket innerApi() {
    	
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("innerApi")
                .genericModelSubstitutes(DeferredResult.class)
//                .genericModelSubstitutes(ResponseEntity.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zybros.jinlh"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(innerApiInfo());
    }

    @Bean
    public Docket openApi() {
    	
    	
    	Predicate predicate = new Predicate() {
            @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;
                return false;
            }
        };
    	
    	
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("openApi")
                .genericModelSubstitutes(DeferredResult.class)
//              .genericModelSubstitutes(ResponseEntity.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(false)
                .select()
                .apis(predicate)
                .paths(PathSelectors.any())//过滤的接口
                .build()
                .apiInfo(openApiInfo());
    }

    private ApiInfo innerApiInfo() {
        return new ApiInfoBuilder()
            .title("JinLH inner Platform API")//大标题
            .description("内部api")//详细描述
            .version("1.0")//版本
            .termsOfServiceUrl("NO terms of service")
            .contact(new Contact("stone", "https://www.jinlh.com", "[email protected]"))//作者
            .license("The Apache License, Version 2.0")
            .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
            .build();
    }

    private ApiInfo openApiInfo() {
        return new ApiInfoBuilder()
            .title("JinLH Platform API")//大标题
            .description("金老虎提供的OpenAPI")//详细描述
            .version("1.0")//版本
            .termsOfServiceUrl("NO terms of service")
            .contact(new Contact("泽佑","www.zybros.com", "[email protected]"))//作者
            .license("The Apache License, Version 2.0")
            .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
            .build();
    }
}

注解  API

@RestController
public class ProductApi extends BaseController {
		
	@ApiOperation(value="根据类别获取商品", notes="需要提供类别ID")
	@RequestMapping(value = "/wx/ctg_product_ctgs_list", method = {RequestMethod.POST,RequestMethod.GET})
	public Map listWithCtgs(HttpServletRequest rqs, Category ctg) throws BusinessException {
		Product p = new Product();
		if (ctg.getLevel() != null) {
			if (ctg.getLevel() == 1) {
				p.setCategoryId1(ctg.getId());
			} else if (ctg.getLevel() == 2) {
				p.setCategoryId2(ctg.getId());
			} else if (ctg.getLevel() == 3) {
				p.setCategoryId3(ctg.getId());
			}
		}
		ProductMapper mapper = this.getMybatisBaseService().getMapper(ProductMapper.class);
		p.setShopId(ctg.getShopId());
		p.setStatus(Product.Status.UP);
		List list = mapper.selectList(p);
		Map m = new HashMap();
		m.put("products", list);
		
		CategoryMapper mapper2 = this.getMybatisBaseService().getMapper(CategoryMapper.class);
		ctg.setParentId(0);
		ctg.setStatus(Category.Status.UP);
		List ctgs = mapper2.selectList(ctg);
		m.put("categories", ctgs);
		
		
		m.put("imgHttpPath", SystemConstants.PRODUCT_IMG_HTTP_PATH);
		return m;

	}

	@ApiOperation(value="获取商品信息", notes="需要提供商品ID")
	@RequestMapping(value = "/wx/product", method = {RequestMethod.POST,RequestMethod.GET})
	public Map get(HttpServletRequest rqs, int id) throws BusinessException {
		ProductMapper ccm = this.getMybatisBaseService().getMapper(ProductMapper.class);
		Product product = ccm.selectByPrimaryKey(id);
		Map m = new HashMap();
		m.put("product", product);
		m.put("imgHttpPath", SystemConstants.PRODUCT_IMG_HTTP_PATH);
		return m;
	}

}


swagger UI

innerAPI

spring boot swagger 分组 定制 显示API_第1张图片



openAPI

spring boot swagger 分组 定制 显示API_第2张图片


你可能感兴趣的:(配置管理)