我正在尝试在验证输入时发现字段错误时自定义BindingResult错误消息。
我目前正在这样做
@ApiOperation(value = "Add an Employee")
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
public void addEmployee(@Valid @RequestBody EmployeeDto e, BindingResult bindingResult) {
if (bindingResult.hasFieldErrors()) {
List bindingErrors = bindingResult.getFieldErrors();
for (FieldError error : bindingErrors) {
String field = error.getField();
bindingResult.rejectValue(field, "Format invalide! vous devez respecter specs" + field);
}
}
else {
// Employee employee = new Employee();
ModelMapper modelMapper = new ModelMapper();
Employee employee = modelMapper.map(e, Employee.class);
employeeRepository.save(employee);
}
}
我在dto中有这个
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
// @DateTimeFormat(pattern = "dd-MM-yyyy")
// @Pattern(regexp = "^(0[1-9]|[12][0-9]|3[01])[-](0[1-9]|1[012])[-](19|20)$")
private Date birthDate;
看不到我的消息,我总是收到默认消息!