一、话不多说,先看效果
1.Swagger页面这样,这里就不多做介绍
2.增强后的首页
3.API列表
4.API请求参数
5.API响应参数
个人觉得增强后看着很清晰,比原来的要好很多。
二、开始配置
1.pom.xml 配置
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.10.5</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.7</version>
</dependency>
配置中遇到的问题: springfox-swagger2这个的版本号不同会导致无法导入@EnableSwagger2WebMvc这个注解
2.配置Swagger2Config
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.EnableSwagger2WebMvc;
/**
*@类名 Swagger2Config
*@描述 配置Swagger2
*@版本 1.0
*@创建人 XuKang
*@创建时间 2021/7/16 13:29
**/
@Configuration
@EnableSwagger2WebMvc
public class Swagger2Config {
/**
* @method createRestApi
* @desc 配置SWAGGER参数
* @version V1.0.0
* @author xukang
* @date 2021/7/16 13:30
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("test")//分组名
.select()
.apis(RequestHandlerSelectors.basePackage("com.demo.controller"))//定位到自己的接口所在包的路径
.paths(PathSelectors.any())
.build();
}
/**
* @method apiInfo
* @desc 配置ApiInfo
* @version V1.0.0
* @author xukang
* @date 2021/7/16 13:30
* @return ApiInfo
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//.title("Spring Boot中使用Swagger2构建RESTful APIs")
.title("标题")
.description("简介")
.termsOfServiceUrl("http://localhost:8080")
.contact(new Contact("作者名称","http://localhost:8080","123@163.com"))
.version("1.0")
.build();
}
}
3.配置WebMvcConfig
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.spring.web.SpringfoxWebMvcConfiguration;
/**
*@类名 WebMvcConfig
*@描述 文件配置
*@版本 1.0
*@创建人 XuKang
*@创建时间 2021/12/28 14:22
**/
@SpringBootApplication
@ConditionalOnClass(SpringfoxWebMvcConfiguration.class)
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
4.开发环境yml文件可以打开增强配置
打开之后可以根据官网自定义一下组件,例如排序等
knife4j:
enable: true #开启配置增强
setting:
enableVersion: true
5.生产环境yml文件可以屏蔽
knife4j:
enable: true #开启配置增强
# 开启Swagger的Basic认证功能,默认是false
# 开启生产环境屏蔽
production: true
6.controller注解配置
@RestController
@RequestMapping("wechat/bank/")
@Api(value = "微信银行所用工具相关组件", tags = "3.微信微信银行相关组件")
public class LkWeChatBankController{
}
对应的效果如下
7.entity实体类配置
import com.alibaba.fastjson.annotation.JSONField;
import com.jingmai.video.live.pay.providers.model.vo.entity.LkWeChatBanksBranchesData;
import com.jingmai.video.live.pay.providers.model.vo.entity.LkWeChatLinksVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* @类名 LkWeChatBanksBranchesResponseVO
* @描述 查询支行列表响应实体
* @版本 1.0
* @创建人 XuKang
* @创建时间 2022/1/12 14:00
**/
@Data
@ApiModel(value="LkWeChatBanksBranchesResponseVO",description="查询支行列表响应实体")
public class LkWeChatBanksBranchesResponseVO implements Serializable {
private static final long serialVersionUID = -7098096518017171297L;
@JSONField(name = "total_count")
@ApiModelProperty(value = "查询数据总条数",name = "totalCount",example = "10",required=true,position = 1)
private int totalCount;
@ApiModelProperty(value = "本次查询数据条数",name = "count",example = "10",required=true,position = 2)
private int count;
@ApiModelProperty(value = "-支行列表",name = "data",required=false,position = 3)
private List<LkWeChatBanksBranchesData> data;
@ApiModelProperty(value = "本次查询偏移量",name = "offset",example = "0",required=true,position = 4)
private String offset;
@ApiModelProperty(value = "分页链接",name = "links",required=true,position = 5)
private LkWeChatLinksVO links;
@JSONField(name = "account_bank")
@ApiModelProperty(value = "开户银行",name = "accountBank",example = "招商银行其他银行",required=true, position = 6)
private String accountBank;
@JSONField(name = "account_bank_code")
@ApiModelProperty(value = "开户银行编码",name = "accountBankCode",example = "1001",required=true, position = 7)
private int accountBankCode;
@JSONField(name = "bank_alias")
@ApiModelProperty(value = "银行别名",name = "bankAlias",example = "工商银行深圳前海微众银行",required=true, position = 8)
private String bankAlias;
@JSONField(name = "bank_alias_code")
@ApiModelProperty(value = "银行别名编码",name = "bankAliasCode",example = "1000006247",required=true, position = 9)
private String bankAliasCode;
}
8.Controller方法配置
@PostMapping("getBankingBranches/{bankAliasCode}")
@ApiOperationSupport(order = 5)
@ApiOperation(value="查询支行列表", notes="本接口可以用于根据银行别名编码(仅支持需要填写支行的银行别名编码)和城市编码过滤查询支行列表数据")
@ApiImplicitParams({
@ApiImplicitParam(name = "weChatBanksBranchesRequestVO", value = "查询支行列表请求实体", required = true, dataType = "LkWeChatBanksBranchesRequestVO"),
@ApiImplicitParam(name = "bankAliasCode", value = "银行别名编码", required = true,dataType = "String",example = "1000006247",paramType = "path")
})
public Result<LkWeChatBanksBranchesResponseVO> getBankingBranches(@RequestBody LkWeChatBanksBranchesRequestVO weChatBanksBranchesRequestVO,
@PathVariable(value = "bankAliasCode") String bankAliasCode){
return null;
别的注解应该可以看懂,自己看吧
三、总结
总结个棒棒锤,自己去官网看,1.6 快速开始 | knife4j