表单验证
当前端传到后台后,表单的数据都要经过校验
@Data public class OrderForm { /** * 买家姓名 */ @NotEmpty(message = "姓名必填") private String name; /** * 买家手机号 */ @NotEmpty(message = "手机号必填") private String phone; /** * 买家地址 */ @NotEmpty(message = "地址必填") private String address; /** * 买家微信openid */ @NotEmpty(message = "openid必填") private String openid; /** * 购物车 */ @NotEmpty(message = "购物车不能为空") private String items; }
后台controller层:
//创建订单 @PostMapping("/create") public ResultVO<Map<String, String>> create(@Valid OrderForm orderForm, BindingResult bindingResult) { if (bindingResult.hasErrors()) { log.error("【创建订单】参数不正确, orderForm={}", orderForm); throw new SellException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage()); }
}