------------恢復內容開始------------
1.創建SpringBoot項目
2.選擇快捷方式創建springboot項目
3.工程文件樹形圖
4.pom.xml中導入Swagger依賴
代碼如下:
1 <dependency> 2 <groupId>io.springfox</groupId> 3 <artifactId>springfox-swagger2</artifactId> 4 <version>2.9.2</version> 5 </dependency> 6 <dependency> 7 <groupId>io.springfox</groupId> 8 <artifactId>springfox-swagger-ui</artifactId> 9 <version>2.9.2</version> 10 </dependency>
5.配置Swagger信息
代碼如下:
1 package com.example.dell; 2 3 import org.springframework.context.annotation.Bean; 4 import org.springframework.context.annotation.Configuration; 5 import springfox.documentation.builders.ApiInfoBuilder; 6 import springfox.documentation.builders.PathSelectors; 7 import springfox.documentation.builders.RequestHandlerSelectors; 8 import springfox.documentation.service.Contact; 9 import springfox.documentation.spi.DocumentationType; 10 import springfox.documentation.spring.web.plugins.Docket; 11 import springfox.documentation.swagger2.annotations.EnableSwagger2; 12 13 /** 14 * @program:swagger2-demo 15 * @description:swagger配置類 16 * @author:feixiang 17 * @create:2020/5/14 18 */ 19 20 @Configuration 21 @EnableSwagger2 22 public class Swagger2Config { 23 @Bean 24 public Docket createRestApi() { 25 return new Docket(DocumentationType.SWAGGER_2) 26 .pathMapping("/") 27 .select() 28 .apis(RequestHandlerSelectors.basePackage("com.example.dell.web")) 29 .paths(PathSelectors.any()) 30 .build().apiInfo(new ApiInfoBuilder() 31 .title("飛翔用接口自動化平台") 32 .description("飛翔獨享") 33 .version("9.0") 34 .contact(new Contact("馬化騰","blog.csdn.net","aaa@gmail.com")) 35 .license("The Apache License") 36 .licenseUrl("http://www.baidu.com") 37 .build()); 38 } 39 }
創建接口
接下來就是創建接口了,Swagger2相關的注解其實並不多,而且很容易懂,下面我來分別向小伙伴們舉例說明
代碼如下
1 package com.example.dell.web; 2 3 import com.example.dell.pojo.User; 4 import io.swagger.annotations.ApiImplicitParam; 5 import io.swagger.annotations.ApiImplicitParams; 6 import io.swagger.annotations.ApiOperation; 7 import org.springframework.web.bind.annotation.*; 8 import io.swagger.annotations.Api; 9 import org.springframework.web.bind.annotation.PathVariable; 10 import org.springframework.web.bind.annotation.RequestBody; 11 import org.springframework.web.bind.annotation.RequestMapping; 12 import org.springframework.web.bind.annotation.RestController; 13 14 15 16 @RestController 17 @Api(tags = "用戶管理相關接口") 18 @RequestMapping("/user") 19 public class UserController{ 20 @PostMapping("/") 21 @ApiOperation("添加用戶的接口") 22 @ApiImplicitParams({ 23 @ApiImplicitParam(name = "username", value = "用戶名", defaultValue = "張三"), 24 @ApiImplicitParam(name = "address", value = "用戶地址", defaultValue = "佛山", required = true) 25 } 26 ) 27 public RespBean addUser(String username, @RequestParam(required = true) String address) { 28 return new RespBean(); 29 } 30 31 @GetMapping("/") 32 @ApiOperation("根據id查詢用戶的接口") 33 @ApiImplicitParam(name = "id", value = "用戶id", defaultValue = "99", required = true) 34 public User getUserById(@PathVariable Integer id) { 35 User user = new User(); 36 user.setId(id); 37 return user; 38 } 39 @PutMapping("/{id}") 40 @ApiOperation("根據id更新用戶的接口") 41 public User updateUserById(@RequestBody User user) { 42 return user; 43 } 44 }
這里邊涉及到多個API,我來向小伙伴們分別說明:
@Api注解可以用來標記當前Controller的功能。
@ApiOperation注解用來標記一個方法的作用。
@ApiImplicitParam注解用來描述一個參數,可以配置參數的中文含義,也可以給參數設置默認值,這樣在接口測試的時候可以避免手動輸入。
如果有多個參數,則需要使用多個@ApiImplicitParam注解來描述,多個@ApiImplicitParam注解需要放在一個@ApiImplicitParams注解中。
需要注意的是,@ApiImplicitParam注解中雖然可以指定參數是必填的,但是卻不能代替@RequestParam(required = true),前者的必填只是在Swagger2框架內必填,拋棄了Swagger2,這個限制就沒用了,所以假如開發者需要指定一個參數必填,@RequestParam(required = true)注解還是不能省略。
如果參數是一個對象(例如上文的更新接口),對於參數的描述也可以放在實體類中。例如下面一段代碼:
1 package com.example.dell.pojo; 2 3 import io.swagger.annotations.ApiModelProperty; 4 5 6 7 8 public class User { 9 @ApiModelProperty(value = "用戶id") 10 private Integer id; 11 @ApiModelProperty(value = "用戶名") 12 private String username; 13 @ApiModelProperty(value = "用戶地址") 14 private String address; 15 //getter/setter 16 17 public Integer getId() { 18 return id; 19 } 20 21 public void setId(Integer id) { 22 this.id = id; 23 } 24 25 public String getUsername() { 26 return username; 27 } 28 29 public void setUsername(String username) { 30 this.username = username; 31 } 32 33 public String getAddress() { 34 return address; 35 } 36 37 public void setAddress(String address) { 38 this.address = address; 39 } 40 }
代碼如下:
1 package com.example.dell.web; 2 3 import io.swagger.annotations.ApiModelProperty; 4 5 public class RespBean { 6 @ApiModelProperty(value = "用戶id") 7 private Integer id; 8 @ApiModelProperty(value = "用戶名") 9 private String username; 10 @ApiModelProperty(value = "用戶地址") 11 private String address; 12 //getter/setter 13 14 public Integer getId() { 15 return id; 16 } 17 18 public void setId(Integer id) { 19 this.id = id; 20 } 21 22 public String getUsername() { 23 return username; 24 } 25 26 public void setUsername(String username) { 27 this.username = username; 28 } 29 30 public String getAddress() { 31 return address; 32 } 33 34 public void setAddress(String address) { 35 this.address = address; 36 } 37 }
然后啟動,運行
此時啟動項目,輸入http://localhost:8080/swagger-ui.html,能夠看到如下頁面,說明已經配置成功了: