界面风格: 这里我进行了分组 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();
}
}