1、引入依赖
io.springfox springfox-swagger2 2.5.0 io.springfox springfox-swagger-ui 2.5.0
2、swagger类的编写
package com.autohome.fc.loan.dealer.provider.config; import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.base.Predicate; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.RequestHandler; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration //让spring加载该类 @EnableSwagger2 //启动swagger public class Swagger2 { @Bean public Docket createRestApi() {// 创建API基本信息 return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("XX.provider.controller)) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() {// 创建API的基本信息,这些信息会在Swagger UI中进行显示 return new ApiInfoBuilder() .title("xx")// API 标题 .description("维护在springboot中的api")// API描述 .contact("xx")// 联系人 .version("1.0")// 版本号 .build(); } }
3、接口响应实体对象
@ApiModel(value = "录单信息") public class AccountInfoAPIDto { @ApiModelProperty(value = "录单人") private String inputBy; @ApiModelProperty(value = "录单人名称") private String inputName; @ApiModelProperty(value = "车商|合作网点编 码") private String dealerCode; @ApiModelProperty(value = "车商|合作网点名 称") private String dealerName; @ApiModelProperty(value = "代理人编码") private String agentCode; @ApiModelProperty(value = "代理人名称") private String agentName;
4、post请求接受参数
@ApiOperation(value = "信息录入接口") @Override public ResponseDto submitApply(@ApiParam @RequestBody SubmitApplyRequestDto submitApplyRequestDto) { }
5、get请求接受参数
@ApiOperation(value="发送短信获取验证码") @ApiImplicitParams({ @ApiImplicitParam(name = "flowid", value = "流程号", required = true, paramType = "query", dataType = "String"), @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "String") }) @Override public ResponseDto applyVerifyCode(String flowId,String userId) { }
6、实现扫描多个包路径
@Configuration //让spring加载该类 @EnableSwagger2 //启动swagger public class Swagger2 { // 定义分隔符 private static final String splitor = ";"; @Bean public Docket createRestApi() {// 创建API基本信息 return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(basePackage("XX.provider.controller"+splitor+"XX.provider.web.insurance")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() {// 创建API的基本信息,这些信息会在Swagger UI中进行显示 return new ApiInfoBuilder() .title("XX")// API 标题 .description("XX")// API描述 .contact("XX")// 联系人 .version("1.0")// 版本号 .build(); } public static PredicatebasePackage(final String basePackage) { return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true); } private static Function , Boolean> handlerPackage(final String basePackage) { return input -> { // 循环判断匹配 for (String strPackage : basePackage.split(splitor)) { boolean isMatch = input.getPackage().getName().startsWith(strPackage); if (isMatch) { return true; } } return false; }; } private static Optional extends Class>> declaringClass(RequestHandler input) { return Optional.fromNullable(input.declaringClass()); } }
7、完成上述代码添加上,启动Spring Boot程序,访问:http://localhost:8080/swagger-ui.html