I am new to Spring and I am writing an API ,where it can consume upto 71 values in the POJO . What i would like to do is check for 20 fields in the POJO which are mandatory and if they are null , I want to add custom error message saying this a mandatory filed and it cant be Null(I have a custom class where i can add Error messages ) . How can i do this , is there a way to loop over this items , is there a way where we cann add annotations like @NotNULL
or i need to manually do an if else check on this items if they are null and add the error message . I was writing if else cases checking whether they are empty and i got really curious to simplify and reduce the lines of code
Asked
Active
Viewed 498 times
0

Praveen
- 225
- 1
- 4
- 18
-
1http://stackoverflow.com/questions/4805168/customize-spring-validation-error – Darshan Mehta Jan 20 '17 at 23:30
-
I am pretty sure you just want to have POJO field validation using javax.validation.constraints.*;. From what I am reading this should help. If you have more information I can give you a better example. @NotNull(message="Name is mandatory") private String name; Let me know what you are using on the front end to capture and display the error message. Good Luck! – Dan Hargis Jan 21 '17 at 02:29
-
I am going to expose it purely as an API . No front end . Could you shed some more light on the javax.validation.constraint – Praveen Jan 21 '17 at 02:45
-
Eclipse shows me an error on message part – Praveen Jan 21 '17 at 05:55
1 Answers
1
I would go with @NotNull or a custom constraint (Custom constraints). As for the loop you could also do that but i would deprecate it. Check out the java.lang.reflect
package to learn more.