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
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.
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