- All Implemented Interfaces and Traits:
- MessageSourceAware
@CompileStatic
class ResponseFormattingUnknownExceptionHandler
extends Object
implements MessageSourceAware
Handles shaping and internationalizing the body in HTTP responses when the execution of request results in throwing non-anticipated exception.
Produced HTTP response body is a JSON serialized from OperationResponse instance containing populated metaData
and empty payload
properties. Here is an example:
{
"metaData": {
"general": {
"severity": "error",
"locale": "en_GB",
"timestamp": "2021-02-09T10:09:36.354151Z"
},
"http": {
"status": "500",
"message": "Internal Server Error"
},
"violation": {
"code": "500",
"message": "Internal server error.",
"type": "unknown",
"logUuid": "116be9a6-9f38-4954-8b8f-e57e781655d0"
}
},
"payload": {}
}
Property metaData.violation.message
needs to be localized.
When used from the Spring Boot application, the easiest is to create controller advice that is eligible for component scanning (@ControllerAdvice annotation is annotated with @Component):
@ControllerAdvice
class ResponseFormattingUnknownExceptionHandlerControllerAdvice extends ResponseFormattingUnknownExceptionHandler {
}
For localization purposes, we are defining responseFormattingDefaultMessages
resource bundle containing default messages. In the Spring Boot application, that resource bundle needs to be
configured, for example, in application.yml
file:
...
spring.messages.basename: messages,responseFormattingDefaultMessages
...
Localization message codes for metaData.violation.message
property is created with
MessageSourceResolvableHelper.makeMessageCodeListForViolationMessageOfUnknownFailure method, where you can look
for further details. - See Also:
- MessageSourceResolvableHelper
- MessageSourceResolvableHelper.makeMessageCodeListForViolationMessageOfUnknownFailure