Dynamic message for custom exception, annotated as ResponseStatus
I am trying to provide a dynamic message for my custom exception, for example in the code snippet below:
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Entity not found")
public class EntityNotFoundException extends RuntimeException {
public EntityNotFoundException(String msg) {
super(msg);
}
}
But always when I throw it like below:
throw new EntityNotFoundException("User entity not found");
in the browser, I get the message "Entity not found" instead of "Custom entity not found".
How to do it?
+3
source to share
1 answer
I'm stuck on this, but I just removed the @ResponseStatus reason and it works, so your code should look something like this:
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class EntityNotFoundException extends RuntimeException {
public EntityNotFoundException(String msg) {
super(msg);
}
}
And now you can set a custom message by the constructor
+2
source to share