How to use different error codes for validation messages?

Marcel Overdijk

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.

M. Deinum

The default message is the message as stated by the programmer. In the case of those JSR-303 annotations probably the ones as Hibernate thought of them. The default message including the code is passed to the MessageSource.getMessage method, which contains a parameter defaultMessage

When you look at the Errors object or actually the ObjectError method you will see that it implements the MessageSourceResolvable interface. This means you can just pass the error, as is, to the MessageSource, including the Locale if you want.

@RequestMapping
public Object foo(@Valid @RequestBody MyObject obj, Errors errors, Locale locale) {
    for (ObjectError err : errors.getAllErrors()) {
        String msg = messageSource.getMessage(err, locale);
        // Do something with msg
    }
}

Something like the above should resolve the message. The advantage of using the ObjectError itself is that next to the code you also have the parameters (for @Range or @Min) which you can then use in your message with placeholders.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java - How to setup validation with different error messages

From Dev

Use two validation messages in different places for JSF SelectOneMenu

From Dev

jquery-validation-unobtrusive: Display Different Error Messages in Summary and Label

From Dev

Rails: validation error codes in JSON

From Dev

How to customize different error messages for the same field?

From Dev

Should a RESTful API return different status codes or error messages for token expired and unauthorized?

From Dev

scalaz: how to handle different error types in a Validation?

From Dev

How to use Twitter Boootstrap's form validation tooltip messages?

From Dev

How to use validation_messages and display_exceptions in Apigility?

From Dev

Sendgrid: how to use different tracking settings for different sets of messages

From Dev

Laravel 5.1 How to output HTML in form validation error messages

From Dev

How to override OmniFaces default validation/conversion error messages?

From Dev

How do I combine resource strings for validation attribute error messages?

From Dev

How to push only validation error messages into an array using Laravel

From Dev

How should a Formly validation function contribute to error messages?

From Dev

How to change `Email validation` error messages dynamically in jquery

From Dev

Spring Boot how to return my own validation constraint error messages

From Dev

How to change `Email validation` error messages dynamically in jquery

From Dev

How to use find command and filter error messages

From Dev

Bootstrap Validation Error Messages CSS

From Dev

Implementing data validation with error messages

From Dev

php form validation and error messages

From Dev

How to use and interpret MPI-IO Error codes?

From Dev

Simple_Form display validation error messages next to different input field

From Dev

Standard error codes for json schema validation

From Dev

validation with different error classes

From Dev

ActiveModel: different validation messages per view

From Dev

How to localize Bean Validation messages

From Dev

How to show the validation messages together

Related Related

  1. 1

    Java - How to setup validation with different error messages

  2. 2

    Use two validation messages in different places for JSF SelectOneMenu

  3. 3

    jquery-validation-unobtrusive: Display Different Error Messages in Summary and Label

  4. 4

    Rails: validation error codes in JSON

  5. 5

    How to customize different error messages for the same field?

  6. 6

    Should a RESTful API return different status codes or error messages for token expired and unauthorized?

  7. 7

    scalaz: how to handle different error types in a Validation?

  8. 8

    How to use Twitter Boootstrap's form validation tooltip messages?

  9. 9

    How to use validation_messages and display_exceptions in Apigility?

  10. 10

    Sendgrid: how to use different tracking settings for different sets of messages

  11. 11

    Laravel 5.1 How to output HTML in form validation error messages

  12. 12

    How to override OmniFaces default validation/conversion error messages?

  13. 13

    How do I combine resource strings for validation attribute error messages?

  14. 14

    How to push only validation error messages into an array using Laravel

  15. 15

    How should a Formly validation function contribute to error messages?

  16. 16

    How to change `Email validation` error messages dynamically in jquery

  17. 17

    Spring Boot how to return my own validation constraint error messages

  18. 18

    How to change `Email validation` error messages dynamically in jquery

  19. 19

    How to use find command and filter error messages

  20. 20

    Bootstrap Validation Error Messages CSS

  21. 21

    Implementing data validation with error messages

  22. 22

    php form validation and error messages

  23. 23

    How to use and interpret MPI-IO Error codes?

  24. 24

    Simple_Form display validation error messages next to different input field

  25. 25

    Standard error codes for json schema validation

  26. 26

    validation with different error classes

  27. 27

    ActiveModel: different validation messages per view

  28. 28

    How to localize Bean Validation messages

  29. 29

    How to show the validation messages together

HotTag

Archive