Custom message for overflowed number during binding in Spring

I have a web application written in Spring. There is an HTML form with one Integer field. I added a custom post for typeMismatch.java.lang.Integer and it handled correctly, but there is one problem. Spring does not distinguish between string value and overflowed integer. In other words, it makes no difference whether the user enters this is a string or 1000000000000000000000000000 . Both are treated by Spring as typeMismatch . I would like to have two separate posts for both cases.

I thought of two solutions:

  • replace java.lang.Integer with java.math.BigInteger in form - then the typeMismatch error will only apply to this is a string , and I will process 1000000000000000000000000000 into the validator
  • register your own property editor for Integer , but I'm not sure that I can handle two different errors with the same property editor

Do you have any better concepts for this problem?

+4
source share
1 answer

Could you integrate the hibernate / Java EE6 bean validation check into your Spring project?

Then you can limit the valid range allowed for your integer field using annotation, for example:

 @Min(1) @Max(10000) private Integer myInteger; 

Here is a simple article:

http://www.c-sharpcorner.com/UploadFile/5fd9bd/javax-annotation-and-hibernate-validator-a-pragmatic-appro/

0
source

Source: https://habr.com/ru/post/1214456/


All Articles