我的春季启动应用程序就像中间人一样。它等待请求,然后格式化此请求并发送到服务器并将服务器响应返回给请求发送方。但是,当我从服务器收到响应错误响应时(例如状态代码为400 Bad Request)我想通过添加以JSON格式从服务器返回的错误原因来修改默认的spring boot JSON异常体。
来自服务器的响应:
Http status: 400
{
"type": "InvoiceDto",
"currency": "EUR",
"error_code": "NO_AMOUNT"
"error_message": "amount is not set"
"invoice_status": "FAILED",
"payment_id": "20516324",
"order_id": 1209,
}
Spring boot返回异常:
{
"timestamp": 1493211638359,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.web.client.HttpClientErrorException",
"message": "400 Bad Request",
"path": "/sms"
}
我想编辑spring的例外字段" message"服务器返回" error_message"值。但似乎我甚至无法获得Response body,因为spring boot会自动抛出默认异常。
答案 0 :(得分:1)
我的理解是你需要提供自己的异常映射器。现在使用的是自动配置添加的默认ErrorController
。
正确的方法是定义自己的ResponseEntityExceptionHandler
您可以阅读有关自定义Exceptionmappers here
的信息