当前位置:   article > 正文

Swagger学习及运用(三)--Swagger注解文档及常用参数配置运用_swagger 参数

swagger 参数

Swagger注解介绍及常用参数配置与运用

1 @API

    作用于类,放置于controller的一个类上,标志这个类是Swagger资源。资源影响Swagger的根文档、资源列表和该特定资源的API声明。swagger将只包含和声明使用@Api注释的类,而忽略其他资源(servlet等)

1.1 参数

参数名称说明类型默认值备注
tags说明Sting""当tags为""或者未指定使用默认值时,显示为@API标注的类名,如UserInfoController显示为user-info-controller
description描述String""当description为""或者未指定使用默认值时,显示为@API标注的类名,如UserInfoController显示为User Info Controller

 

1.2 实例代码

  1. @Api(
  2. tags = "用户",
  3. description = "User Info Controller"
  4. )
  5. @Controller
  6. @RequestMapping("/user")
  7. public class UserInfoController {
  8. ...
  9. }

1.3 示例图

 

2 @ApiOperation

    描述API方法,作用于方法上面,放置于标注为@API注解类的方法上,用户表示一个http的请求

2.1 参数

参数名称说明类型默认值备注
value方法说明String""必填
httpMethod请求方法String"" 
produces输出类型String"" 
response返回类型Class<?>java.lang.Void.class 
notes方法描述详情String"" 

 

2.2 实例代码

  1. @ApiOperation(
  2. value = "根据ID查找用户信息接口", httpMethod = "GET",
  3. produces = "application/json",response = Dto.class,
  4. notes = "根据ID查找用户信息"+
  5. "<p>成功:success = ‘true’ | 失败:success = ‘false’ 并返回错误码,如下:</p>" +
  6. "<p>错误码:</p>"+
  7. "<p>10000 : 用户信息不存在 </p>"
  8. )
  9. @RequestMapping(value="/select/{id}",method = RequestMethod.GET)
  10. @ResponseBody
  11. public Dto<Object> selectUserinfoByid(@PathVariable("id") Integer id){
  12. Userinfo userinfo = userinfoService.selectUserinfoById(id);
  13. if(userinfo != null){
  14. return DtoUtil.returnSuccess("查询用户信息成功");
  15. }
  16. return DtoUtil.returnFail("用户信息不存在","10000");
  17. }

2.3 示例图

 

3 @ApiImplicitParams

    多个参数描述,作用于标注@ApiOperation注解的方法上,解释请求参数里边添加@ApiImplicitParam()注解

3.1 参数

参数名称说明类型默认值备注
name参数名称String""当name使用""后者未指定使用默认值时,显示为参数指定的参数名
value参数描述String""当value使用""后者未指定使用默认值时,显示为参数指定的参数名
paramType参数类型String""当paramType使用""后者未指定使用默认值时,根据参数的实际来源显示,如果来源于请求路径则显示为path
require是否必需booleanfalse 

 

3.2 实例代码

  1. @ApiOperation(
  2. value = "根据ID查找用户信息接口", httpMethod = "GET",
  3. produces = "application/json",response = Dto.class,
  4. notes = "根据ID查找用户信息"+
  5. "<p>成功:success = ‘true’ | 失败:success = ‘false’ 并返回错误码,如下:</p>" +
  6. "<p>错误码:</p>"+
  7. "<p>10000 : 用户信息不存在 </p>"
  8. )
  9. @ApiImplicitParams(
  10. @ApiImplicitParam(name = "id", value = "用户ID", paramType = "path", required = true)
  11. )
  12. @RequestMapping(value="/select/{id}",method = RequestMethod.GET)
  13. @ResponseBody
  14. public Dto<Object> selectUserinfoByid(@PathVariable("id") Integer id){
  15. Userinfo userinfo = userinfoService.selectUserinfoById(id);
  16. if(userinfo != null){
  17. return DtoUtil.returnSuccess("查询用户信息成功");
  18. }
  19. return DtoUtil.returnFail("用户信息不存在","10000");
  20. }

3.3 示例图

 

 

4 @ApiParam

    描述API方法,,作用于标注@ApiOperation注解的方法的参数上,用于参数字段的说明

4.1 参数

参数名称说明类型默认值备注
name参数名称String""当name使用""后者未指定使用默认值时,显示为参数指定的参数名
value参数描述String""当value使用""后者未指定使用默认值时,显示为参数指定的参数名
required是否必需booleanfalse 

 

4.2 实例代码

  1. @ApiOperation(
  2. value = "根据ID查找用户信息接口", httpMethod = "GET",
  3. produces = "application/json",response = Dto.class,
  4. notes = "根据ID查找用户信息"+
  5. "<p>成功:success = ‘true’ | 失败:success = ‘false’ 并返回错误码,如下:</p>" +
  6. "<p>错误码:</p>"+
  7. "<p>10000 : 用户信息不存在 </p>"
  8. )
  9. @RequestMapping(value="/select/{id}",method = RequestMethod.GET)
  10. @ResponseBody
  11. public Dto<Object> selectUserinfoByid(@ApiParam(name = "id",value = "用户ID",required = true) @PathVariable("id") Integer id){
  12. Userinfo userinfo = userinfoService.selectUserinfoById(id);
  13. if(userinfo != null){
  14. return DtoUtil.returnSuccess("查询用户信息成功");
  15. }
  16. return DtoUtil.returnFail("用户信息不存在","10000");
  17. }

4.3 示例图

 

5 @ApiModel

    作用于类,对类进行说明,用于实体类接收或者返回

5.1 参数

参数名称说明类型默认值备注
value对象名String"" 
description对象描述String"" 

 

5.2 实例代码

  1. @ApiModel(value = "角色Model",description = "角色表实体对象")
  2. public class Roleinfo {
  3. @ApiModelProperty(
  4. value = "角色ID",
  5. name = "roleId",
  6. dataType = "Integer",
  7. required = true
  8. )
  9. private Integer roleId;
  10. @ApiModelProperty(
  11. value = "角色名称",
  12. name = "roleName",
  13. dataType = "String",
  14. required = true
  15. )
  16. }

5.3 示例图

5 @ApiModelProperty

    作用于实体类,用于实体类中某个字段,,用对象接收参数时,描述对象的一个字段

5.1 参数

参数名称说明类型默认值备注
value参数解释String"" 
name参数名称String"" 
dataType属性类型String"" 
required是否必填booleanfalse 
example示例String  
hidden隐藏booleanfasle 

 

5.2 实例代码

  1. @Api(
  2. tags = "用户",
  3. description = "User Info Controller"
  4. )
  5. @Controller
  6. @RequestMapping("/user")
  7. public class UserInfoController {
  8. @Autowired
  9. private UserinfoService userinfoService;
  10. @ApiOperation(
  11. value = "新增用户信息接口", httpMethod = "POST",
  12. produces = "application/json", response = Dto.class,
  13. notes = "新增用户信息" +
  14. "<p>成功:success = ‘true’ | 失败:success = ‘false’ 并返回错误码,如下:</p>" +
  15. "<p>错误码:</p>" +
  16. "<p>10001 : 添加用户信息失败 </p>"
  17. )
  18. @RequestMapping(value = "/insert", method = RequestMethod.POST)
  19. @ResponseBody
  20. public Dto<Object> insertUserinfo(Userinfo userinfo){
  21. int row = userinfoService.insert(userinfo);
  22. if(row > 0){
  23. return DtoUtil.returnSuccess("添加用户信息成功");
  24. }
  25. return DtoUtil.returnFail("添加用户信息失败", "10002");
  26. }
  27. }
  1. @ApiModel(value = "用户Model")
  2. public class Userinfo {
  3. @ApiModelProperty(
  4. value = "用户id",
  5. name = "id",
  6. dataType = "Integer",
  7. required = false
  8. )
  9. private Integer userId;
  10. @ApiModelProperty(
  11. value = "账号",
  12. name = "userAccount",
  13. dataType = "String",
  14. required = true
  15. )
  16. private String userAccount;
  17. @ApiModelProperty(
  18. value = "密码",
  19. name = "userPassword",
  20. dataType = "String",
  21. required = true
  22. )
  23. private String userPassword;
  24. @ApiModelProperty(
  25. value = "用户名",
  26. name = "userName",
  27. dataType = "String",
  28. required = false
  29. )
  30. private String userName;
  31. @ApiModelProperty(
  32. value = "年龄",
  33. name = "userAge",
  34. dataType = "Integer"
  35. )
  36. private Integer userAge;
  37. ...
  38. getter and setter
  39. ...
  40. }

5.3 示例图

附件:测试源码

链接:https://pan.baidu.com/s/1N_wxYxJpX5rVF4drF69C_A 
提取码:ew5d 

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

闽ICP备14008679号