一、与模型相关的注解,用在bean上面java
@ApiModel:用在bean上,对模型类作注释;restful
@ApiModelProperty:用在属性上,对属性作注释app
二、与接口相关的注解post
@Api:用在controller上,对controller进行注释;ui
@ApiOperation:用在API方法上,对该API作注释,说明API的做用;url
@ApiImplicitParams:用来包含API的一组参数注解,能够简单的理解为参数注解的集合声明; spa
@ApiImplicitParam:用在@ApiImplicitParams注解中,也能够单独使用,说明一个请求参数的各个方面,该注解包含的经常使用选项有:rest
paramType:参数所放置的地方,包含query、header、path、body以及form,最经常使用的是前四个。code
name:参数名;orm
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);
}
|