Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3655

How to handle Internal server error (500) on spring rest API?

$
0
0

Good day,

I am working on spring rest api and i would like to sure everything is working fine. I would like to log abnormal behaviors like nullPointerException or database connection error or any Exception that could raise and not handled or not assumed.

I would like to catch any unhandled exception and show a beautifull message to user instead of printing stack trace.

for this i found a solution on internet that is extend ResponseEntityExceptionHandler and override handleExceptionInternal method.

I also like to log 404 errors to see if someone trying to attack on my server.

I have also added this line in properties file : spring.mvc.throw-exception-if-no-handler-found=true

and here is the code for handleExceptionInternal

@Overrideprotected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {    GenericResponse response = new GenericResponse();    response.setMessage("Internal error occured, "+ ex.getStackTrace()[0]);    System.out.println("big exceptions");    return new ResponseEntity(response, headers, status);}

My problem is when i am passing incorrect route like /abc this code is running fine, But when i throw null pointer exception from controllers method this method is not catching it.

thanks.


Viewing all articles
Browse latest Browse all 3655

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>