Since I have just started off with Spring boot, I'm creating a very basic project with very basic rest controllers. The intent is to understand the basics before trying out any complicated code so the examples below are very basic. There is no complex business logic, just minimal lines of code.
I have created a basic project with a simple rest controller, it has the following annotations
@RestController
@RequestMapping("users")
public class UserController
{
In the get method I have intentionally created a null pointer exception by assigning null to a variable and then checking the length.
@GetMapping(path="/{UserId}",produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<UserViewDTO> getUser(@PathVariable String UserId)
{
String firstName = null;
int len_fname = firstName.length();
UserViewDTO responseBody= new UserViewDTO();
responseBody.setFirstname("1stname");
responseBody.setLastname("lastname");
responseBody.setEmail("email@gmail.com");
responseBody.setUserid(UserId);
return new ResponseEntity<UserViewDTO>(responseBody,HttpStatus.OK);
}
As a result of this I get the bellow message in response in Postman.
{
"timestamp": "2021-03-07T19:56:48.790+00:00",
"status": 500,
"error": "Internal Server Error",
"exception": "java.lang.NullPointerException",
"message": "No message available",
"path": "/users/1212121"
}
I wanted to do handle this with @Controller advice from a central class. I created the following
import org.apache.catalina.WebResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@ControllerAdvice
public class AppExceptionHandler extends ResponseEntityExceptionHandler
{
@ExceptionHandler(value= {Exception.class})
public ResponseEntity<Object> handleAnyException(Exception ex, WebRequest request)
{
return new ResponseEntity<>(
ex, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
When I put a debugger the code under the @ControllerAdvice does not even get activated. Also here is my application.properties code
server.error.include-exception=true
server.error.include-message=always
server.error.include-binding-errors=always
What am I doing wrong over here ?