Swagger 2 (http://swagger.io/) 是一个用来生成RESTful APIs描述文档的开源项目。下面介绍如何用Swagger 2生成spring boot的RESTful API文档
maven依赖如下
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
spring boot中使用spring mvc声明的restful api如下
package guru.springframework.controllers;
@RestController
@RequestMapping("/product")
public class ProductController {
private ProductService productService;
@Autowired
public void setProductService(ProductService productService) {
this.productService = productService;
}
@RequestMapping(value = "/list", method= RequestMethod.GET)
public Iterable list(Model model){
Iterable productList = productService.listAllProducts();
return productList;
}
@RequestMapping(value = "/show/{id}", method= RequestMethod.GET)
public Product showProduct(@PathVariable Integer id, Model model){
Product product = productService.getProductById(id);
return product;
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public ResponseEntity saveProduct(@RequestBody Product product){
productService.saveProduct(product);
return new ResponseEntity("Product saved successfully", HttpStatus.OK);
}
@RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
public ResponseEntity updateProduct(@PathVariable Integer id, @RequestBody Product product){
Product storedProduct = productService.getProductById(id);
storedProduct.setDescription(product.getDescription());
storedProduct.setImageUrl(product.getImageUrl());
storedProduct.setPrice(product.getPrice());
productService.saveProduct(storedProduct);
return new ResponseEntity("Product updated successfully", HttpStatus.OK);
}
@RequestMapping(value="/delete/{id}", method = RequestMethod.DELETE)
public ResponseEntity delete(@PathVariable Integer id){
productService.deleteProduct(id);
return new ResponseEntity("Product deleted successfully", HttpStatus.OK);
}
}
配置swagger
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import static springfox.documentation.builders.PathSelectors.regex;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("guru.springframework.controllers")) // guru.springframework.controllers包下的
.paths(regex("/product.*"))// 路径为product开头的
.build()
.apiInfo(metaData());
}
private ApiInfo metaData() {
ApiInfo apiInfo = new ApiInfo(
"Spring Boot REST API",
"Spring Boot REST API for Online Store",
"1.0",
"Terms of service",
new Contact("John Thompson", "https://springframework.guru/about/", "john@springfrmework.guru"),
"Apache License Version 2.0",
"https://www.apache.org/licenses/LICENSE-2.0");
return apiInfo;
}
}
这里用@EnableSwagger2打开Swagger,并配置了处理哪个包下的什么路径,以及meta信息。
配置好后启动,访问http://localhost:8080/v2/api-docs可以看到Json格式的api描述
访问 http://localhost:8080/swagger-ui.html 可看到Swagger UI生成的RESTful APIs描述文档。可以看到应用定义了哪些controller和meta信息。
选中controller并点击进入如下界面
可以看到controller下详细的Restful api信息。
还可以对Restful api进行更详细的配置,比如在代码中加了如下声明
@ApiOperation(value = "View a list of available products", response = Iterable.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully retrieved list"),
@ApiResponse(code = 401, message = "You are not authorized to view the resource"),
@ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
@ApiResponse(code = 404, message = "The resource you were trying to reach is not found")
}
)
@RequestMapping(value = "/list", method= RequestMethod.GET, produces = "application/json")
public Iterable list(Model model){
Iterable productList = productService.listAllProducts();
return productList;
}
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@ApiModelProperty(notes = "The database generated product ID")
private Integer id;
@Version
@ApiModelProperty(notes = "The auto-generated version of the product")
private Integer version;
@ApiModelProperty(notes = "The application-specific product ID")
private String productId;
@ApiModelProperty(notes = "The product description")
private String description;
将能生成更为详细的文档。
参考文档 https://dzone.com/articles/spring-boot-restful-api-documentation-with-swagger