当前位置:   article > 正文

Swagger使用介绍二之为Controller中参数添加字段说明_swagger传入参数名与字段名不一致

swagger传入参数名与字段名不一致
  • 回顾

上文介绍了Swagger如何生成API文档--https://blog.csdn.net/shangcunshanfu/article/details/100838137,本篇介绍一下如何在API文档上为参数添加说明。

  • 添加头信息
  1. import cn.hgd11.swagger.success.entity.TestEntity;
  2. import io.swagger.annotations.Api;
  3. import io.swagger.annotations.ApiImplicitParam;
  4. import io.swagger.annotations.ApiImplicitParams;
  5. import io.swagger.annotations.ApiOperation;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. /**
  10. * @author :尚村山夫
  11. * @date :Created in 2019/9/14 23:14
  12. * @modified By:
  13. */
  14. @RestController
  15. @RequestMapping("test3")
  16. @Api(tags = "测试3")
  17. public class Test3Controller {
  18. @ApiImplicitParams({
  19. @ApiImplicitParam(name = "Authorization", value = "认证Token", required = true, dataType = "String",paramType = "header")
  20. })
  21. @GetMapping
  22. @ApiOperation("测试方法1") // 用户对该接口进行说明的注解
  23. public TestEntity testEntity(){
  24. return new TestEntity();
  25. }
  26. }

将@ApiImplicitParam注解的paramType参数指定为header,该参数会告诉Swagger这是一个头信息。

点进@ApiImplicitParam查看paramType参数

  1. /**
  2. * The parameter type of the parameter.
  3. * <p>
  4. * Valid values are {@code path}, {@code query}, {@code body},
  5. * {@code header} or {@code form}.
  6. */
  7. String paramType() default "";

会发现,该参数支持五种类型,header即表示头信息。添加完该参数,API文档展现如下

对于有些添加了认证的系统来说,该参数可以在头信息中添加Token认证。将需要的Token添加到该参数中,即可向服务器发送请求。

  • 添加键值对参数
  1. import cn.hgd11.swagger.success.entity.TestEntity;
  2. import io.swagger.annotations.*;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.springframework.web.bind.annotation.RestController;
  7. /**
  8. * @author :尚村山夫
  9. * @date :Created in 2019/9/14 23:14
  10. * @modified By:
  11. */
  12. @RestController
  13. @RequestMapping("test3")
  14. @Api(tags = "测试3")
  15. public class Test3Controller {
  16. @ApiImplicitParams({
  17. @ApiImplicitParam(name = "Authorization", value = "认证Token", required = true, dataType = "String",paramType = "header"),
  18. })
  19. @GetMapping
  20. @ApiOperation("测试方法1") // 用户对该接口进行说明的注解
  21. public TestEntity testEntity(
  22. @RequestParam(value = "id") @ApiParam(value = "用户ID") Integer id,
  23. @RequestParam(value = "name") @ApiParam(value = "用户名称") String name
  24. ){
  25. return new TestEntity();
  26. }
  27. }

代码中的@ApiParam即是参数说明,添加该参数后,API文档展现如下:

  • 添加键值对参数的别一种方法
  1. import cn.hgd11.swagger.success.entity.TestEntity;
  2. import io.swagger.annotations.*;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.springframework.web.bind.annotation.RestController;
  7. /**
  8. * @author :尚村山夫
  9. * @date :Created in 2019/9/14 23:14
  10. * @modified By:
  11. */
  12. @RestController
  13. @RequestMapping("test3")
  14. @Api(tags = "测试3")
  15. public class Test3Controller {
  16. @ApiImplicitParams({
  17. @ApiImplicitParam(name = "Authorization", value = "认证Token", required = true, dataType = "String",paramType = "header"),
  18. @ApiImplicitParam(name = "id", value = "用户ID", dataType = "integer",paramType = "query"),
  19. @ApiImplicitParam(name = "name", value = "用户姓名", dataType = "String",paramType = "query"),
  20. })
  21. @GetMapping
  22. @ApiOperation("测试方法1") // 用户对该接口进行说明的注解
  23. public TestEntity testEntity(
  24. @RequestParam(value = "id") Integer id,
  25. @RequestParam(value = "name") String name
  26. ){
  27. return new TestEntity();
  28. }
  29. }

前面讲到@ApiImplicitParam的paramType属性有五个类型,其中一个是query,这个参数就是告诉Swagger这里有一个键值对的参数。这里的name属性要和参数名一致,value属性即是对参数的说明。

不过请看userId参数的类型,本来是integer类型,但是被解析成了ref,这里没找到原因。

其实本人比较习惯用第一种键值对的传参方式,直接对应于某一个参数,比较直接。

  • 实体参数

对于POST请求或PUT请求,通常用传入一个实体类型的参数,如User对象,这时候怎么对User实体中的每一个属性进行参数说明呢?看以下代码:

实体1

  1. /**
  2. * @author :尚村山夫
  3. * @date :Created in 2019/8/13 22:55
  4. * @modified By:
  5. */
  6. @ApiModel(description = "测试实体")
  7. public class TestEntity {
  8. @ApiModelProperty(value = "名称")
  9. private String name;
  10. @ApiModelProperty(value = "ID")
  11. private Integer id;
  12. @ApiModelProperty(value = "内部实体")
  13. private TestEntity2 testEntity2;
  14. public TestEntity2 getTestEntity2() {
  15. return testEntity2;
  16. }
  17. public void setTestEntity2(TestEntity2 testEntity2) {
  18. this.testEntity2 = testEntity2;
  19. }
  20. public String getName() {
  21. return name;
  22. }
  23. public void setName(String name) {
  24. this.name = name;
  25. }
  26. public Integer getId() {
  27. return id;
  28. }
  29. public void setId(Integer id) {
  30. this.id = id;
  31. }
  32. }

实体2

  1. import io.swagger.annotations.ApiModel;
  2. import io.swagger.annotations.ApiModelProperty;
  3. /**
  4. * @author :尚村山夫
  5. * @date :Created in 2019/8/13 22:55
  6. * @modified By:
  7. */
  8. @ApiModel(description = "测试实体2")
  9. public class TestEntity2 {
  10. @ApiModelProperty(value = "名称2")
  11. private String name;
  12. @ApiModelProperty(value = "ID2")
  13. private Integer id;
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. public Integer getId() {
  21. return id;
  22. }
  23. public void setId(Integer id) {
  24. this.id = id;
  25. }
  26. }

对实体添加@ApiModel注解,对实体中的每一个属性添加@ApiModelProperty注解,Controller代码如下:

  1. import cn.hgd11.swagger.success.entity.TestEntity;
  2. import io.swagger.annotations.Api;
  3. import io.swagger.annotations.ApiImplicitParam;
  4. import io.swagger.annotations.ApiImplicitParams;
  5. import io.swagger.annotations.ApiOperation;
  6. import org.springframework.web.bind.annotation.*;
  7. /**
  8. * @author :尚村山夫
  9. * @date :Created in 2019/9/14 23:14
  10. * @modified By:
  11. */
  12. @RestController
  13. @RequestMapping("test3")
  14. @Api(tags = "测试3")
  15. public class Test3Controller {
  16. @ApiImplicitParams({
  17. @ApiImplicitParam(name = "Authorization", value = "认证Token", required = true, dataType = "String",paramType = "header"),
  18. })
  19. @PostMapping
  20. @ApiOperation("测试方法1")
  21. public TestEntity testEntity(
  22. @RequestBody TestEntity testEntity
  23. ){
  24. return new TestEntity();
  25. }
  26. }

展现如下:

诈一看有一点点乱,如果你是一名程序员而不是UI,请不要纠结于此。

在此做一个说明,GET请求是不支持接受@RequestBody参数是

到此其实有一个问题,如果要传入的实体是JSONObject类型,并没有一个实体类供我们添加@ApiModel及@ApiModelProperty怎么办呢,请参考https://blog.csdn.net/shangcunshanfu/article/details/100840152Swagger使用介绍三之为Controller中参数添加JSONObject类型字段说明

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/897711
推荐阅读
相关标签
  

闽ICP备14008679号