1、与模型相关的注解,用在bean上面
@ApiModel:用在bean上,对模型类做注释;
@ApiModelProperty:用在属性上,对属性做注释
2、与接口相关的注解
@Api:用在controller上,对controller进行注释;
@ApiOperation:用在API方法上,对该API做注释,说明API的作用;
@ApiImplicitParams:用来包含API的一组参数注解,可以简单的理解为参数注解的集合声明;
@ApiImplicitParam:用在@ApiImplicitParams注解中,也可以单独使用,说明一个请求参数的各个方面,该注解包含的常用选项有:
paramType:参数所放置的地方,包含query、header、path、body以及form,最常用的是前四个。
name:参数名;
dataType:参数类型,可以是基础数据类型,也可以是一个class;
required:参数是否必须传;
value:参数的注释,说明参数的意义;
defaultValue:参数的默认值;
@ApiResponses:通常用来包含接口的一组响应注解,可以简单的理解为响应注解的集合声明;
@ApiResponse:用在@ApiResponses中,一般用于表达一个响应信息
code:即httpCode,例如400
message:信息,例如"操作成功"
response = UserVo.class 这里UserVo是一个配置了@ApiModel注解的对像,该是对像属性已配置 @ApiModelProperty,swagger可以通过这些配置,生 成接口返回值
代码例子:
@ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
@ApiImplicitParam(paramType="path", name = "id", value = "用户ID", required = true, dataType = "Long")
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public User getUser(@PathVariable Long id) {
return users.get(id);
}
网上好多例子都没有paramType这个参数,导致获取不到URL的参数,特意记录一下
详细的注解说明
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@ApiOperation(value="添加博客", notes="新增博客") @ApiImplicitParams({ @ApiImplicitParam(name = "mess", value = "博客内容", required = true, paramType = "query", dataType = "String") }) @RequestMapping(value = "/addblog", method = RequestMethod.POST) public Result addBlog(@RequestBody Blog blog, @RequestParam(name = "mess", required = true)String mess, HttpServletRequest request){ Integer userId = (Integer) request.getSession().getAttribute("userId"); userId = 12; Integer effecNum = blogService.addBlog(blog, mess, userId); if(effecNum > 0) return new Result(ResultEnums.SUCCESS); return new Result(ResultEnums.PARAM_IS_INVAILD); } |
原文:https://www.cnblogs.com/jtlgb/p/10637920.html