spring mvc 注解整理(一)
@Controller和@RestController: RestController = @ResponseBody + @Controller 所有返回都是json类型,无法跳转到jsp页面,但是不用在每个方法前加@ReaponseBody
@ApiOperation 说明:
value:函数说明
notes:函数用途
response:返回值类型
@RequestMapping(value = "/v1/apis", method = RequestMethod.POST)
value:访问路径
method:请求方式
@Validated:配合pojo类中的注解使用@NotBlank,用与校验属性的空值,后面必须加 BindingResult bindingResult
Insert.class:表示插入时校验
@RequestBody 将json 转成对象
@ApiParam(value = "Created api object", required = true)
value:参数说明
required:参数是否能为空
1 @ApiOperation(value = "创建api", notes = "根据ApisRequestVo请求对象创建api", response = RequestResult.class) 2 @RequestMapping(value = "/v1/apis", method = RequestMethod.POST) 3 @ResponseBody 4 public ModelAndView createApi( 5 @Validated({Insert.class}) @RequestBody @ApiParam(value = "Created api object", 6 required = true) ApisRequestVo vo, 7 BindingResult bindingResult) { 8 LOGGER.debug("create api "); 9 RequestResult result = null; 10 /* 11 * Map<String, List<String>> errorMsg = parseErrorMsg(bindingResult); 12 * 13 * if (errorMsg != null) { result = new RequestResult(); result.setErrorMsg(errorMsg); return 14 * createModelAndView(result); } 15 */ 16 vo.setId(StringUtils.EMPTY); 17 result = new RequestResult(apiService.createApiService(vo)); 18 return createModelAndView(result); 19 }
@PathVariable(value = "id", required = true) String id)
用来接收restful请求方式的参数
1 @ApiOperation(value = "删除api", notes = "根据id删除 api", response = RequestResult.class) 2 @RequestMapping(value = "/v1/apis/{id}", method = RequestMethod.DELETE) 3 public ModelAndView deleteApiById(@ApiParam(value = "api Id", 4 required = true) @PathVariable(value = "id", required = true) String id) { 5 RequestResult result = new RequestResult(apiService.deleteApi(id)); 6 return createModelAndView(result); 7 }
@NotBlank(message = "apiId不能为空", groups = { Insert.class, Update.class })
校验参数.配合@Validated使用
@ApiModelProperty(required = true, value = "apiId", dataType = "String"
required:是否可以为空
value:属性说明
dataType:属性类型
public class ApisConfig implements Serializable{ private static final long serialVersionUID = 1L; @NotBlank(message = "apiId不能为空", groups = { Insert.class, Update.class }) @ApiModelProperty(required = true, value = "apiId", dataType = "String") private String apiId;