Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic message for custom exception annotated as ResponseStatus

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?

like image 617
Victor Dombrovsky Avatar asked Aug 05 '15 15:08

Victor Dombrovsky


1 Answers

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

like image 170
JFer Avatar answered Sep 21 '22 13:09

JFer