Ang*_*izC 4 error-handling spring-mvc httpresponse spring-boot
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);
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
HttpMessage
Cle*_*lha 11
从 SpringBoot 2.3 开始,这是默认行为,您可以在此处看到。
假设您使用的是 spring 2.3+
server.error.include-message=always
Run Code Online (Sandbox Code Playgroud)
在您的 application.properties 中会为您解决问题。
| 归档时间: |
|
| 查看次数: |
1926 次 |
| 最近记录: |