I want to write Binding result without JSP file and I wrote like this. But I get same result every time
@RequestMapping(value = "/submit", method = RequestMethod.POST) public ResponseEntity<?> submit(@RequestBody Employee employee,BindingResult bindingResult) throws EmployeeNotFoundException { if (!bindingResult.hasErrors()) return new ResponseEntity<>(employeeRepository.save(employee), HttpStatus.CREATED); else{ return new ResponseEntity<>(" not working", HttpStatus.CONFLICT); } }
Advertisement
Answer
Looks like you missed javax.validation.Valid
annotation for your request body :
public ResponseEntity<?> submit(@Valid @RequestBody Employee employee,BindingResult bindingResult) throws EmployeeNotFoundException {
Once you have annotated your method with Valid
annotation, your request body will get validated in controller layer & any validation related errors can be seen in your else block of if (!bindingResult.hasErrors())
.
Tip : You can also throw org.springframework.web.bind.MethodArgumentNotValidException
in absence of BindingResult
in your method argument.