0

I'm trying to validate a numeric variable in the request, what I'm trying to achieve, for this field to be non null and numeric. I want to have different errors reported for null and conversion error.

I was trying to use org.springframework.format.annotation.NumberFormat

Why doesn't the @NumberFormat doesn't have default message property? Is there a reason why this has been missed. I'll now have to customize it as I'm not using the message resource bundles.

public class AddToJobsShortListWSRequest implements Serializable {

@NumberFormat(style = NumberFormat.Style.NUMBER)
@NotNull(message="ASL01")
private Long userDetailId;

controller

public ResponseEntity<String> handlePostRequest(String xmlRequest, String... externalIds) {

    ResponseEntity<String> response = null;
    Set<Enum> enums = new HashSet<Enum>();
    AddToJobsShortListWSRequest addToJobsShortListWSRequest = serializationDeserializationSupport.fromString(xmlRequest, AddToJobsShortListWSRequest.class);

    if(!jsonRequestValidator.validate(AddToJobsShortListWSError.class, enums, addToJobsShortListWSRequest))
    {
        response = getBadRequestErrorResponseEntity(enums);
    }  
    else{
.....
} 

Validator

 private void validate(@SuppressWarnings("rawtypes") Class enumClass, Object object, @SuppressWarnings("rawtypes") Set<Enum> enums) {
    BindException errors = new BindException(object, "object");

    validator.validate(object, errors);

    @SuppressWarnings({"rawtypes"})
    List fieldErrors = errors.getFieldErrors();

    for (int i = 0; i < fieldErrors.size(); i++) {
        if (fieldErrors.get(i) instanceof FieldError) {
            String m = ((FieldError) fieldErrors.get(i)).getDefaultMessage();
            enums.add(Enum.valueOf(enumClass, m));
        }
    }
}

Is there any other annotation based validation applicable here? Also, what is the order of validation, which one kicks in first, NumberFormat, NotNull?

Thanks in advance :)

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
Taran Singh
  • 583
  • 2
  • 6
  • 13

1 Answers1

1

It wasn't missed - the validation is just more simple than you need and Spring only supports message resource bundles as i18n facility.

So for your special case - it might not be very special for you - you need a special implementation.

Also there is no guaranteed order for validations. Each validation must cope with all the cases that it doesn't handle (so the number validation must work with empty and null values because other validations will check that).

I, for one, find the annotation based validation useful for very simple hello-world type cases like @NotNull or @NotEmpty. For everything else, I prefer commons validator plus my own annotations because that allows me to define the patterns and common validations that I need for my apps and run them in a defined order if I have to.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • You are right, I just realized if that is the case - Spring only supports message resource bundles as i18n facility. so that's where it's different from jsr-303 Thanks – Taran Singh Mar 26 '12 at 15:15