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 :)