How to handle Internal server error (500) on spring rest API? : <p>Good day,</p>

<p>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.</p>

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

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

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

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

<p>and here is the code for handleExceptionInternal</p>

<pre><code>@Override
protected ResponseEntity&lt;Object&gt; 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);

}
</code></pre>

<p>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.</p>

<p>thanks.</p>
