I'm trying to provide dynamic message for my custom Exception like in 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 shown below:
throw new EntityNotFoundException("User entity not found");
in the browser, I get the message "Entity not found" instead of "User entity not found".
How to achieve this?
I was stuck on this, But I just removed the reason side of the @ResponseStatus and It works, so your code should be like this:
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class EntityNotFoundException extends RuntimeException {
public EntityNotFoundException(String msg) {
super(msg);
}
}
And now you can set custom message by the constructor
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With