2

I have a sample CRUD application, The application used is a Wine Cellar app. You can search for wines, add a wine to your cellar, update and delete wines. I got it from RESTful services with jQuery and Java using JAX-RS and Jersey.

I modified the Wine class to include validation constraints.

@NotNull(message='Name must have a value')
private String name;
@NotNull(message='Grapes must have a value')
private String grapes;

If the user creates/updates, a wine the errors will be thrown if the name and grape fields are empty. All my validation messages are returned to the browser in json format.

public Wine create(Wine wine) {...}
public Wine update(Wine wine) {...}

If only one error is thrown, I want to display the correct message to the user and also highlight the field.

How do I get the empty field(name or id) that triggered the error as well as the correct validation message?

heyomi
  • 385
  • 5
  • 18
  • Can you give some more details on how do you invoke validation? Are you doing it yourself or is there some integration with Bean Validation provided by Jersey? – Gunnar Apr 08 '13 at 11:30
  • I'm using Bean Validation. I did some more searching and found this question http://stackoverflow.com/questions/5226797/making-javax-validation-error-message-more-specific , it seems similar to what I want to accomplish. I'll review it and update my question, if needed. – heyomi Apr 08 '13 at 16:32
  • I'm still not sure how Bean Validation is invoked in your case. Are you invoking `javax.validation.Validator` yourself? – Gunnar Apr 08 '13 at 22:45

1 Answers1

0

Sorry about inconsistencies or for lack of clarity.

I used an Web Application Exception Mapper to handle the exceptions. It checks if the error was generated from a web application exception or constraint violation exception.

if (exception instanceof ConstraintViolationException) {
   Set<ErrorResponse> errorResponses = new HashSet<>();
   for (ConstraintViolation violation : ((ConstraintViolationException) exception).getConstraintViolations()) {
   errorResponses.add(new ErrorResponse(violation.getPropertyPath().toString(),violation.getMessage()));
   }
   builder.entity(new WebApplicationError(errorResponses));
}

Also, I checked if was a JsonMapping Exception

if (exception instanceof JsonMappingException) {
           ResourceBundle bundle = ResourceBundle.getBundle("ValidationMessages");
           Set<ErrorResponse> errorResponses = new HashSet<>();

       for(Reference ref : ((JsonMappingException) exception).getPath()) {
          String className = ref.getFrom().getClass().getName()
                                        .substring(ref.getFrom().getClass().getName()
                                        .lastIndexOf(".") + 1).toLowerCase();                        
          String key = "the.key";
          try {
               message = bundle.getString(key);
          } catch (MissingResourceException e) {
               logger.error(e.getMessage());
          }
               errorResponses.add(new ErrorResponse(ref.getFieldName(), message));
        }
            builder.entity(new WebApplicationError(errorResponses));
    }

JsonMapping Exception

Constraint Violation

heyomi
  • 385
  • 5
  • 18