I have written the below Custom Error Handler in SPRING BOOT
@RestControllerAdvice
public class CustomGlobalExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler
// @ResponseBody
public ResponseEntity<CustomErrorResponse> customHandleNotFound(Exception ex, WebRequest request) {
CustomErrorResponse errors = new CustomErrorResponse();
errors.setTimestamp(LocalDateTime.now());
errors.setError(ex.getMessage());
errors.setStatus(HttpStatus.CONFLICT.value());
errors.setErrorMsg(errors.getErrorMsg());
return ResponseEntity.ok(errors);
}
Below is my code Controller method
@RestController
@RequestMapping(value="/api")
public class AppController {
@Autowired
private UserRepository userRepository;
@RequestMapping(value="/home/{id1}" ,method=RequestMethod.PUT)
@ResponseBody
public String home(@PathVariable("id1") Long idValue, @RequestBody @Valid Student s)
{
System.out.println( " idValue is " + idValue);
System.out.println( " Student -->" + s.getName() + " -- " + s.getId());
return "job done";
}
@RequestMapping("/foos")
// @ResponseBody
public String getFoos(@RequestParam(value="t1",required=true) String id) {
int i =1/0;
System.out.println(" id is " + i++);
return "ID: " + id + " status: Success";
}
The exception handler is working fine for the method getFoos() when it encounters 1/0 I get the output in POSTMAN as which is as expected
{
"timestamp": "2019-08-04 11:24:22",
"status": 409,
"error": "/ by zero",
"errorMsg": "ERROR-msg"
}
But in my home() method I deliberately made Student object as invalid and I get no error message in POSTMAN but just in the eclipse console. Why?
I get this message.So how do I kick in CustomGlobalExceptionHandler when the Student object is invalid
2019-08-04 23:25:57.551 WARN 9652 --- [nio-8080-exec-6] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.String com.example.hibernatemapping.controller.AppController.home(java.lang.Long,com.example.hibernatemapping.domain.Student)]