Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot @ResponseStatus not returning HTTP message

I've a problem returning HTTP Messages when an exception is thrown. I'm using @ResponseStatus annotation to handle the HTTP Status code, it shows ok but the message is ignored.

Custom Exception:

@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR, reason = "An error ocurred while trying to 
retrieve the instruments.")
public class InstrumentsNotFoundException extends RuntimeException { 

private static final Logger logger = LoggerFactory.getLogger(InstrumentsNotFoundException.class);

public InstrumentsNotFoundException(String errorMessage) {
    super(errorMessage);
    logger.error(errorMessage);
}

Controller:

@GetMapping({ "/portfolio/" })
public List<Instrument> getAll() {
    try {
        List<Instrument> instruments= portfolioYieldProcessor.getPortfolio();
        return instruments;
    } catch(RuntimeException e) {
        throw new ResponseStatusException(
                  HttpStatus.INTERNAL_SERVER_ERROR, "Sorry, an error occurred, please try again later.", e);
    }   
}

HttpMessage

Http Response

like image 442
AngelRuizC Avatar asked Jun 26 '20 15:06

AngelRuizC


1 Answers

Starting from SpringBoot 2.3 this is the default behavior, as you can see here.

Assuming you are using spring 2.3+

server.error.include-message=always

in your application.properties will do the trick for you.

like image 174
Cleto Gadelha Avatar answered Oct 19 '22 12:10

Cleto Gadelha