Spring Boot 2.X - Spring Boot整合Swagger2
Spring Boot能夠快速開發應用程序,而Swagger2則能夠快速的生成Restful APIs接口文檔。本文將就兩者進行簡單的整合應用。
文中Spring Boot版本爲2.1.4.RELEASE
,Swagger2版本爲2.9.2
添加依賴
利用Spring Initializr快速創建一個Spring Boot應用,並添加Swagger2依賴,主要依賴如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
創建Swagger2配置
@SpringBootConfiguration
@EnableSwagger2
public class SwaggerConfig {
public Docket buildDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.xudc.swagger.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Swagger構建Restful API文檔")
.description("文檔除了查看API還可以進行調試")
.contact(new Contact("xudc", "https://blog.csdn.net/xudc0521", "e-mail"))
.version("1.0.0")
.build();
}
}
新建實體
@Data
@Accessors(chain = true)
public class User {
private Integer id;
private String name;
private Integer age;
}
創建接口並構建文檔內容
@RestController
@RequestMapping("/user")
public class UserController {
private static Map<Integer, User> userMap = Maps.newConcurrentMap();
private final static String SUCCESS = "success";
@ApiOperation("獲取用戶列表")
@GetMapping("")
public List<User> list(){
return Lists.newArrayList(userMap.values());
}
@ApiOperation(value = "創建用戶",notes = "根據User對象創建用戶")
@ApiImplicitParam(name = "user",value = "用戶實體json",required = true,dataType = "User")
@PostMapping("")
public String save(@RequestBody User user) {
userMap.put(user.getId(), user);
return SUCCESS;
}
@ApiOperation(value = "查詢用戶",notes = "根據id查找用戶")
@ApiImplicitParam(name = "id", value = "用戶id", required = true, dataType = "int")
@GetMapping("/{id}")
public User find(@PathVariable Integer id){
User user = userMap.get(id);
return user;
}
@ApiOperation(value = "刪除用戶",notes = "根據id刪除指定用戶")
@ApiImplicitParam(name = "id", value = "用戶id", required = true, dataType = "int")
@DeleteMapping("/{id}")
public String delete(@PathVariable Integer id) {
userMap.remove(id);
return SUCCESS;
}
@ApiOperation(value = "修改用戶",notes = "根據id修改用戶信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用戶id", required = true, dataType = "int"),
@ApiImplicitParam(name = "user", value = "用戶實體json", required = true, dataType = "User")
})
@PutMapping("/{id}")
public String update(@RequestBody User user, @PathVariable Integer id){
User u = userMap.get(id);
u.setName(user.getName()).setAge(user.getAge());
userMap.put(id, u);
return SUCCESS;
}
}
訪問接口文檔
啓動服務,訪問http://localhost:8080/swagger-ui.html
:
點擊user-controller進入接口調試,點擊對應功能右上角的Try it out
進入調試
- 添加用戶(編寫號json後,點擊下方的Execute執行):
- 查詢list:
項目地址
完整代碼:Github