当前位置:   article > 正文

25.JavaWeb-接口文档Swagger_java接口文档swagger

java接口文档swagger

1.Swagger

        swagger是一款可以根据resutful风格生成的生成的接口开发文档,并且支持做测试的一款中间软件。

1.1 接口文档

        接口文档是用于描述API的一份文档,它包含了API的详细信息,包括API的请求和响应参数、接口路径、请求方法、数据类型、返回数据结构等。

2.swagger常用注解

  1. @RestController
  2. @Api(tags = "图书管理API", description = "用于管理图书馆中图书的API")
  3. public class BookController {
  4. @ApiOperation(value = "根据ID获取图书", notes = "根据图书ID从图书馆中获取图书")
  5. @ApiImplicitParam(name = "id", value = "图书ID", required = true, dataType = "Long", paramType = "path")
  6. @GetMapping("/books/{id}")
  7. public ResponseEntity<Book> getBookById(@PathVariable Long id) {
  8. // 省略业务逻辑
  9. }
  10. @ApiOperation(value = "更新图书", notes = "更新图书馆中现有的图书")
  11. @ApiImplicitParams({
  12. @ApiImplicitParam(name = "id", value = "图书ID", required = true, dataType = "Long", paramType = "path"),
  13. @ApiImplicitParam(name = "book", value = "图书对象", required = true, dataType = "Book", paramType = "body")
  14. })
  15. @PutMapping("/books/{id}")
  16. public ResponseEntity<Book> updateBook(@PathVariable Long id, @RequestBody Book book){
  17. // 省略业务逻辑
  18. }
  19. }
注解位置说明
@Api类上用于描述该类是"图书管理API",说明整个API的主要作用。
@ApiOperation方法上用于给API方法增加说明,描述每个方法的作用。例如,getBookById方法用于根据ID获取图书,addBook方法用于添加新图书。
@ApiImplicitParam方法入参用于给方法的入参增加说明,描述参数的作用和数据类型。在getBookById和deleteBook方法中,都有一个参数id,通过该注解说明id的作用和数据类型。
@ApiImplicitParams方法上用于包含一组参数说明,可以在一个注解中同时描述多个参数。在updateBook方法中,使用该注解描述两个参数:id表示图书ID,book表示图书对象。
  1. @Data
  2. @ApiModel(value = "封装商品信息对象")
  3. public class Goods implements Serializable {
  4. @ApiModelProperty(value = "商品id",dataType = "int")
  5. private Integer id;
  6. }
@ApiModel用在返回对象类上,描述一个Model的信息。在本例中,Book类可能是一个POJO类,用于描述图书的信息。
@ApiModelProperty用于描述一个model的属性。在本例中,如果Book类有其他属性,可以使用该注解为这些属性增加说明。例如,书名、作者、出版日期等。

3.使用Swagger

        使用swagger时,只需要在需要生成接口文档的handler中使用相关注解,swagger就可以自动生成接口文档

3.1 导入swagger依赖

  1. <!--swagger-->
  2. <dependency>
  3. <groupId>io.springfox</groupId>
  4. <artifactId>springfox-swagger2</artifactId>
  5. <version>2.9.2</version>
  6. </dependency>
  7. <!--springfox-swagger-ui
  8. Springfox Swagger: Spring 基于swagger规范,可以将基于SpringMVC和Spring Boot
  9. 项目的项目代码,自动生成JSON格式的描述文件。-->
  10. <dependency>
  11. <groupId>io.springfox</groupId>
  12. <artifactId>springfox-swagger-ui</artifactId>
  13. <version>2.9.2</version>
  14. </dependency>

3.2 编写swagger配置类

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import springfox.documentation.builders.ApiInfoBuilder;
  4. import springfox.documentation.builders.PathSelectors;
  5. import springfox.documentation.builders.RequestHandlerSelectors;
  6. import springfox.documentation.service.ApiInfo;
  7. import springfox.documentation.spi.DocumentationType;
  8. import springfox.documentation.spring.web.plugins.Docket;
  9. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  10. @Configuration
  11. @EnableSwagger2
  12. public class SwaggerConfiguration {
  13. @Bean
  14. public ApiInfo apiInfo() {
  15. return new ApiInfoBuilder().title("商品模块API文档")
  16. .description("采用restful风格接口")
  17. .version("1.0")
  18. .build();
  19. }
  20. @Bean
  21. public Docket docket(ApiInfo apiInfo) {
  22. return new Docket(DocumentationType.SWAGGER_2)
  23. .apiInfo(apiInfo)
  24. .select()
  25. .apis(RequestHandlerSelectors.basePackage(
  26. "com.mall.mall100.controller"))
  27. .apis(RequestHandlerSelectors.any())
  28. .paths(PathSelectors.any())
  29. .build();
  30. }
  31. }

4.访问swagger接口文档

http://localhost:9090/swagger-ui.htmlicon-default.png?t=N6B9http://localhost:8080/swagger-ui.html

image-20210820100803455

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号