目录
1. 概述
可以参考以前做过的项目mwp模块中的查询接口,json转ReqBean时根据reqbean中的校验注解进行参数校验
在基于springboot项目时,可以借助于springboot + validator在controller层实现请求参数的自动校验功能。如下controller类中的请求方法:
/**
* 在controller层,自动实现接口参数校验功能
*/
@RequestMapping(method = {RequestMethod.POST}, value = "/personDetailInfoQuery")
public RspBean<R> npfList(@RequestBody @Validated ReqBean<PersonReqBean> reqBean, HttpServletRequest request) throws Exception {
PageUtils page = service.queryPage(reqBean);
RspBean<R> rspBean = new RspBean<>();
rspBean.setMsgBody(R.ok().put("page", page));
rspBean.setMsgHead(MsgHead.defaultMsgHeaderInstance());
return rspBean;
}
在实际项目中,遇到先将请求参数json接收,然后在后面逻辑转为不同reqBean时进行参数校验,这样就需要手动实现上面的校验逻辑,可借助于如下工具类,实现参数校验
import javax.validation.ConstraintViolation;
import javax.va