当前位置:网站首页>Usage of swagger and common annotation explanation
Usage of swagger and common annotation explanation
2022-04-22 05:11:00 【Beicheng Kobayashi】
1、 First add Swagger Dependence
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
2、 Add the corresponding annotation on the class name and method
First of all, let's popularize the commonly used annotations
@Api: Decorate the whole class , describe Controller The role of
@ApiOperation: A method that describes a class , Or an interface
@ApiParam: Single parameter description
@ApiModel: Use objects to receive parameters
@ApiModelProperty: When receiving parameters with objects , A field describing an object
@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: Multiple request parameters
among @ApiImplicitParam There are many attributes of
| attribute | Value | effect |
|---|---|---|
| paramType | Query parameter type | |
| path | Submit data as address @PathVariable Annotation value |
|
| query | Automatically map and assign with parameters @RequestParam Take parameters | |
| body | Submit... As a stream Support only POST @RequestBody Take parameters | |
| from | With form Form submission Support only POST | |
| header | Parameter in request headers Submit it inside | |
| dataType | ||
| Long | ||
| String | The default is String | |
| name | Receive parameter name | |
| value | Description of the meaning of the receiving parameters | |
| required | ||
| true | Expressed as a parameter that must be passed | |
| false | Not a required parameter | |
| defaultValue | The default value is |
3、 Demo code
Annotation of entity class
@Data
public class Student{
@ApiModelProperty(" Student id")
private String Id;
@ApiModelProperty(" The student's name ")
private String name;
@ApiModelProperty(" Student age ")
private int age;
}
Interface annotations The following demonstrates an interface that passes multiple parameters
@Api(value=" Student interface ",description = " Student interface , An increase in providing student information 、 Delete 、 Change 、 check ")
public interface StudentControllerApi {
@ApiOperation(" Search for student information ")
@ApiImplicitParams({ @ApiImplicitParam(name="name",value =" The student's name ",required=true,paramType="path",dataType="String"),
@ApiImplicitParam(name="age",value = "
Student age ",required=true,paramType="path",dataType="int")
})
public Student findStudent(String name , int age) ;
}
Controller Code , The above is only the provider of the interface , Now let's see how to receive the parameters of the interface
@RestController
@RequestMapping("/student/")
public class StudentController implements StudentControllerApi {
@Autowired
private StudentService studentService;
/**
* according to id Inquire about cmsconfig Page information of table
* @param id
* @return
*/
@Override
@GetMapping("/get/{name}/{age}")
public Student findStudent( @PathVariable("name") String name,@PathVariable("age") int age) {
return studentService.findStudent(name,age);
}
}
4、Swagger How interface generation works :
1、 The system starts , Scan to api In the project Swagger2Configuration class
2、 Find the package path specified in this class and start scanning , Find the package and sub package marked with @RestController Annotated controller class
3、 according to controller Class Swagger Annotations generate interface documents .
5、 ... and 、 visit Swagger Page path
After starting the project
View interface documentation , Browser access http://localhost:8080/swagger-ui.html The port path should be changed to its own
版权声明
本文为[Beicheng Kobayashi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210627590327.html
边栏推荐
- Temperature control via mqc582tt + PNET
- Drawing scatter diagram with MATLAB
- Query result processing
- Database 13th job transaction management
- RestFul风格的API设计
- 在线预览PDF文件功能实现
- Libevent implementation client
- 数据库(二)MySQL表的增删改查(基础)
- Introduction to esmodule specification
- Nexus private server - (II) console installation of version 3.2.0, initial password location
猜你喜欢

Chapter IX user and authority

Junit簡介與入門

Speech feature extraction of emo-db dataset

Clonal map of writing in mind

How to restrict Oracle sub query

How to modify the IP address of the rancher server

Programme de démarrage Spark: WordCount

Uninstallation, installation and setting of MySQL

Visio setting network topology

Remote wake-up server
随机推荐
Log4 log framework
Servlet lifecycle
Nodejs link redis
Wos opens the way, and Weimeng makes a new way for SaaS in China
Bugly usage record
Go array slice
How to initiate mqtt 100 million connection and 10 million message throughput performance test
Three paging query methods of SQL Server
Log4 日志框架
Multi level cache architecture for 100 million traffic
Chapter 7 select clause and sub query
2022.04.20 Huawei written examination
Regular expression of shell script
Extraction de la fonction Speech de l'ensemble de données emo - DB
Reset kubernetes cluster
Final Cut Pro mosaics in multiple places of the video at the same time
How does docker access redis service 6379 port in the host
JSON class of Delphi: superobject, and simple usage jsonhelper
Jackson
Layer 3 switch [vlanif details] enable OSPF and router interworking [ENSP implementation]