Spring Boot配置Swagger增强工具
参考knife4j官方网站:1.6 快速开始 | knife4j
一、话不多说,先看效果
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
-
**/
-
-
-
public class Swagger2Config {
-
-
/**
-
* @method createRestApi
-
* @desc 配置SWAGGER参数
-
* @version V1.0.0
-
* @author xukang
-
* @date 2021/7/16 13:30
-
* @return
-
*/
-
-
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
-
**/
-
-
-
public class WebMvcConfig implements WebMvcConfigurer {
-
-
-
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
-
**/
-
-
-
public class LkWeChatBanksBranchesResponseVO implements Serializable {
-
-
private static final long serialVersionUID = -7098096518017171297L;
-
-
-
-
private int totalCount;
-
-
-
private int count;
-
-
-
private List<LkWeChatBanksBranchesData> data;
-
-
-
private String offset;
-
-
-
private LkWeChatLinksVO links;
-
-
-
-
private String accountBank;
-
-
-
-
private int accountBankCode;
-
-
-
-
private String bankAlias;
-
-
-
-
private String bankAliasCode;
-
-
}
@ApiModel注解:
value:实体value,在写接口的和@ApiImplicitParam(dataType={})对应
description:实体简介
@ApiModelProperty注解:
value:字段说明
name:对应的字段
example:示例值
required:是否必填
position:排序
8.Controller方法配置
-
-
-
-
-
-
-
-
public Result<LkWeChatBanksBranchesResponseVO> getBankingBranches( LkWeChatBanksBranchesRequestVO weChatBanksBranchesRequestVO,
-
String bankAliasCode){
-
return null;
@ApiOperationSupport:排序
别的注解应该可以看懂,自己看吧
三、总结
总结个棒棒锤,自己去官网看,1.6 快速开始 | knife4j
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgefhba
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01