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

界面风格: 这里我进行了分组 API 和 H5
在这里插入图片描述

1 引入相关jar包

<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<version>2.2.2</version>
</dependency>
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger-ui</artifactId>
	<version>2.2.2</version>
</dependency>
<dependency>
	<groupId>com.github.xiaoymin</groupId>
	<artifactId>swagger-bootstrap-ui</artifactId>
	<version>1.9.6</version>
</dependency>

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();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值