当前位置:网站首页>How to use swagger2
How to use swagger2
2022-06-22 23:40:00 【Front end plasterer】
Swagger It provides us with a set of methods to automatically generate documents through code and comments , This is a guarantee API The timeliness of documentation will help a lot . Can help us design 、 structure 、 Record and use Rest api
- Introduce dependencies
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
- take swagger Configuration injection spring
/** * @Configuration * The first declaration is the configuration file class * @EnableSwagger2 * Turn on swagger function */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
/** * Use swagger You need to create a summary .Docket * The summary parameters are as follows : * Type of document :DocumentationType.SWAGGER_2 * Documents are made up of a series of selectors :api path * select() Set up apis and paths() * apis Find out which controller The interface of * Get all :RequestHandlerSelectors.any() * If you want to get a specified package :RequestHandlerSelectors.basePackage("com.fanfan.controller") * path Filter in the found interface * @return */
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("com.fanfan.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
/** * Introduction to customized documents * adopt apiInfBuilder establish apiInfo * Parameters can be set title description version wait * @return */
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("spring-boot-swagger")
.description(" This is an easy one demo")
.version("1.0")
.build();
}
}
- Browser open
- http://localhost:8090/swagger-ui.html
Test it :
controller:
/** * @Api Declare the functions of a series of interfaces , Put it in controller above * @ApiOperation Declare the functions of specific interfaces Put it in controller Method above */
@RestController
@Api(tags = " Test related interfaces ", description = " Play test CURD")
public class TestController {
@GetMapping("/test")
@ApiOperation(" The first test interface ")
public void test(){
System.out.println(1);
}
/** * @ApiIgnore Do not show this interface * @param test * @return */
@PostMapping("/add")
@ApiOperation(" Test name ")
public String add(Test test){
return "add";
}
@ApiOperation(" Delete the guest interface ")
@ApiImplicitParam(name = "id", value = " test id")
@PostMapping("/delete")
public String delete(Long id){
return "delete";
}
}
bean file :
/** * @ApiModel(" The guest ") Declaration example * @ApiModelProperty(" name ") Declare the name of the instance property , Parameter display for interface */
@Data
@AllArgsConstructor
@ApiModel(" Test case ")
public class Test {
@ApiModelProperty(" name ")
private String name;
@ApiModelProperty(" role ")
private String role;
}

边栏推荐
- js读取剪切板的图片
- Tianyi cloud takes advantage of the new infrastructure to build a "4+2" capability system for digital transformation
- Digital data was invited to participate in Nantong enterprise digital transformation Seminar
- OJ daily practice - class dining
- Learning the interpretable representation of quantum entanglement, the depth generation model can be directly applied to other physical systems
- Tp5.1 solving cross domain problems
- 2021-05-02
- Using the hbuilder x editor to install a solution for terminal window plug-ins that are not responding
- wallys/WiFi6 MiniPCIe Module 2T2R 2 × 2.4GHz 2x5GHz
- 启牛app下载证券开户,是安全的吗?有风险嘛?
猜你喜欢
随机推荐
2021-04-14
OJ daily practice - delete word suffixes
Autoincrement attribute of sqlserver replication table
Optimization - linear programming
1. class inheritance (point)
js----SVG转PNG
【ARM】讯为rk3568开发板lvds屏设置横屏显示
Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file
node-fetch下载文件
js判断浏览器是否打开了控制台
在Word中自定义多级列表样式
Canvas generate Poster
2. interface (calculator)
Is it safe to make an appointment to pay new debts? Is it reliable?
OJ daily practice - class dining
Common operations of sourcetree version management
Safe and reliable! Tianyi cloud data security management platform passed the evaluation
剑指 Offer 05. 替换空格
Webrtc series - 4connection sorting of network transmission
Spark RDD Programming Guide(2.4.3)







