Controller层
@RestController
@RequestMapping("/car")
public class CarController {
@PostMapping("/list")
public ResultBean<CarVO> list(@Valid @RequestBody QueryCarRequest request, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
// 所有没有通过验证的字段都会保存在bindingResult内
List<FieldError> fieldErrors = bindingResult.getFieldErrors();
// 只取第一个错误消息并返回,比如“车牌不能为空”
String err = fieldErrors.get(0).getDefaultMessage();
ResultBean resultBean = new ResultBean<>();
resultBean.fail(err);
return resultBean;
}
return carBusi.list(request);
}
}
CarRequest传参
@Data