I have a Spring Boot REST server which should return specific error codes when invalid input is provided. I don't need any i18n here, just plain English error codes like 'missing' is sufficient.
Using Spring Boot with Hibernate Validator, after validation I get a back Spring Errors object.
For each error I can get the code and defaultMessage. For a @NotBlank constraint this would return NotBlank and may not be null resp.
Basically I want to translate this error to just missing as I'm not interested in i18n translation. Also other constraints I want to more REST friendly error codes.
I though to use use a simple messages.properties or ValidationMessages.properties inside src/main/resources but this wouldn't work unfortunately. Note I tried both adding a general NotBlank=missing and specific NotBlank.entity.field=missing properties.
I'm not sure why it's not working... maybe because resolving i18n messages (in jsp world) does not go directly via Spring Errors but through the MessageCodesResolver implementation (just guessing).
Probably I could get the error code from the Spring Error and do a lookup from the message code resolver. But I wonder why error.getDefaultMessage does not return the appropriate value form the ValidationMessages.properties.
Any suggestion is welcome.
error.getDefaultMessagegets the default message and that is the message as generated by code not from properties files. If you look at theMessageSource.getMessagemethod you see that thedefaultMessageis passed in. The same mechanism is used when using validation.MessageSourcethe errors as returned from theErrorsobject are all instances ofMessageSourceResolvable. So just iterate over the errors, pass them to theMessageSourceand get the message you want from themessages.properties. Maybe you could gentrify this logic and put it in a class that generates a response object.