spring swagger2 + swagger-bootstrap-ui +swgger分组 。简单几步10分钟轻松搞定

界面风格: 这里我进行了分组 API 和 H5
spring swagger2 + swagger-bootstrap-ui +swgger分组 。简单几步10分钟轻松搞定_第1张图片

1 引入相关jar包


	io.springfox
	springfox-swagger2
	2.2.2


	io.springfox
	springfox-swagger-ui
	2.2.2


	com.github.xiaoymin
	swagger-bootstrap-ui
	1.9.6

2.swagger配置

package cn.steam.config;


import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Created by songyuanbo on 2017/9/15.
 */
@Configuration
@EnableWebMvc
@EnableSwagger2
public class SwaggerConfig {

    @Value("${swagger.enable}")
    private String enableSwagger;

    @Bean
    public Docket h5Docket() {

		// 添加head参数
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("token").description("令牌").modelRef(new 	ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .genericModelSubstitutes(DeferredResult.class)
                .enable("true".equals(enableSwagger))
                .globalOperationParameters(pars)
                .apiInfo(apiInfo())
                .forCodeGeneration(true).select()
                //可以根据包名分组
//                .apis(RequestHandlerSelectors.basePackage("cn.steam.controller.h5"))
				//也可以根据请求路径分组 
                .paths(PathSelectors.ant("/h5/**"))  
                .build().groupName("H5").pathMapping("/");
    }

    @Bean
    public Docket apiDocket() {

		// 添加head参数
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        
        return new Docket(DocumentationType.SWAGGER_2)
                .genericModelSubstitutes(DeferredResult.class)
                .enable("true".equals(enableSwagger))
                .globalOperationParameters(pars)
                .apiInfo(apiInfo())
                .forCodeGeneration(true).select()
                //可以根据包名分组
//                .apis(RequestHandlerSelectors.basePackage("cn.steam.controller"))
				//也可以根据请求路径分组 
                .paths(PathSelectors.ant("/api/**"))
                .build().groupName("API").pathMapping("/");
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("STEAM RESTful APIs")
                .description("pop Resource apis document")
                .termsOfServiceUrl("http://www.xxxxx.cn")
                .version("1.0")
                .build();
    }

}

你可能感兴趣的:(swagger)