当前位置:网站首页>5-integrate swagger2
5-integrate swagger2
2022-07-29 07:22:00 【Zhang Shao】
Swagger Introduce
(1) brief introduction
Swagger Is a specification and complete framework , Used to generate 、 describe 、 Invocation and visualization RESTful Style Web service (https://swagger.io/). Its main function is :
- It makes it more convenient to separate the front end from the back end , It's good for teamwork
- Documentation of the interface is automatically generated online , Reduce the burden of writing interface documents for back-end developers
- A functional test
Spring Have already put Swagger Include your own criteria , Established Spring-swagger project , Now called Springfox. By introducing Springfox , You can use it very simply and quickly Swagger.
(2)SpringBoot Integrate Swagger
- Introduce dependencies , stay zs-blog-model Introduce this dependency into the module
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
Only need zs-blog-common You can configure it in , Because other microservice projects are directly or indirectly dependent .
- stay zs-blog-admin engineering config Add a configuration class to the package
package com.zs.admin.config;
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.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket buildDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(true) // The configuration can be read for control
.apiInfo(buildApiInfo())
.select()
// To scan API(Controller) Basic package
.apis(RequestHandlerSelectors.basePackage("com.zs"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo buildApiInfo() {
Contact contact = new Contact(" Zhang Shao's blog project ","","");
return new ApiInfoBuilder()
.title("zs Blog - Platform management API file ")
.description(" Platform management services api")
.contact(contact)
.version("1.0.0").build();
}
}
(3)Swagger Commonly used annotations
stay Java Class Swagger You can generate Swagger Interface document , Commonly used Swagger The comments are as follows :
[email protected]:== Decorate the whole class , describe Controller The role of
[email protected]:== A method that describes a class , Or an interface
@ApiParam: Description information of a single parameter
@ApiModel: Use objects to receive parameters
@ApiModelProperty: use Object receives parameters when , One that describes the object Field
@ApiResponse:HTTP In response to 1 Description (s)
@ApiResponses:HTTP Respond to the overall description
@ApiIgnore: Use this annotation to ignore this API
@ApiError : Information returned in the event of an error
@ApiImplicitParam: A request parameter
@ApiImplicitParams: Description information of multiple request parameters
@ApiImplicitParam attribute :
attribute | Value | effect |
---|---|---|
paramType | Query parameter type | |
path | Submit data as address | |
query | Automatically map and assign with parameters | |
body | Submit... As a stream Support only POST | |
header | Parameter in request headers Submit it inside | |
form | With form Form submission Support only POST | |
dataType | Data type of parameter As a sign only , There is no actual verification | |
Long | ||
String | ||
name | Receive parameter name | |
value | Description of the meaning of the receiving parameters | |
required | Whether the parameter is required | |
true | Required | |
false | Not required | |
defaultValue | The default value is |
@Api(value = " Channel management ", tags = "channel", description = " Channel management API")
public interface AdChannelControllerApi {
/** * Query the channel list by name * @param dto * @return */
@ApiOperation(value = " Channel paging list query ", notes = " Get a one page channel list ")
public ResponseResult findByNameAndPage(ChannelDto dto);
}
@Data
@Slf4j
public class PageRequestDto {
@ApiModelProperty(value=" The current page ",required = true)
protected Integer size;
@ApiModelProperty(value=" Number of bars per page ",required = true)
protected Integer page;
public void checkParam() {
if (this.page == null || this.page < 0) {
setPage(1);
}
if (this.size == null || this.size < 0 || this.size > 100) {
setSize(10);
}
}
}
@Api(value = " Channel management ", tags = "channel", description = " Channel management API")
public interface AdChannelControllerApi {
/** * Query the channel list by name * @param dto * @return */
@ApiOperation(value = " Channel paging list query ", notes = " Get a one page channel list ")
@ApiImplictParams({
@ApiImplicitParam(name = "pageNum",value = " Page number "),
@ApiImplicitParam(name = "pageSize", value = " The size of each page ")
})
public ResponseResult findByNameAndPage(Integer pageNum,Integer pageSize);
}
边栏推荐
- ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
- 一篇长文---深入理解synchronized
- Spingboot integrates the quartz framework to realize dynamic scheduled tasks (support real-time addition, deletion, modification and query tasks)
- SQL优化
- leetcode力扣经典问题——4.寻找两个正序数组的中位数
- 作业7.28 文件IO与标准IO
- Tp6 use protobuf
- Guess the number / / generate a random number for the first time
- 个人博客系统(附源码)
- Latest 10 billion quantitative private placement list
猜你喜欢
Student status management system based on C language design
亚马逊云助手小程序来啦!
Image noise and matrix inversion
2022-07-28:以下go语言代码输出什么?A:AA;B:AB;C:BA;D:BB。 package main import ( “fmt“ ) func main() { f
WPF simple login page completion case
[Charles' daily problems] when you open Charles, you can't use nails
Vscode remote debugging PHP solution through remotessh and Xdebug
2022-07-28: what is the output of the following go language code? A:AA; B:AB; C:BA; D:BB。 package main import ( “fmt“ ) func main() { f
个人博客系统(附源码)
后缀自动机(SAM)讲解 + Luogu p3804【模板】后缀自动机 (SAM)
随机推荐
Use vscode to configure Mysql to realize connection, query, and other functions
When NPM is installed, it is stuck. There are five solutions
Gin service exit
SpingBoot整合Quartz框架实现动态定时任务(支持实时增删改查任务)
论文阅读 (62):Pointer Networks
fillder使用
计算程序运行时间 demo
3-全局异常处理
MySQL如何把行转换为列?
20-40K| 梅卡曼德3D视觉算法/软件/产品经理招聘
我的个人网站不让接入微信登录,于是我做了这个
能在SQL 语句中 指定 内存参数吗?
WPF简单登录页面的完成案例
Redis基础篇
Redis Basics
CVPR2021| 基于自监督学习的多视图立体匹配 (CVPR2021)
用户列表 圆形头像并跟随小板块
MySQL 高级(进阶) SQL 语句 (一)
Kubernetes (五) ---------部署 Kubernetes Dashboard
同步/异步、阻塞/非阻塞 与 IO