Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating Objects in Spring REST

I am creating a Spring REST web service in java, that can be used by Android/IPhone. My method in the controller is following

@RequestMapping(method = RequestMethod.POST, value = "/registerUser")
    public ModelAndView registerUser(@RequestBody User user,HttpServletRequest request){

//register user

}

I have an idea about Form based validations with Spring validators that are most-closely work with ModelAttribute in case of Spring Websites.

My question is that, How can i validate the user object, and send the specific error to Android/Iphone end? Say if username is not in email pattern , how can i send the error message specific to wrong username to Mobile ends, and how will they display that?

Thanks in advance.

like image 333
Arun Kumar Avatar asked Mar 16 '26 01:03

Arun Kumar


1 Answers

I'd take a look at the JSR303 annotation driven bean validation stuff. This link will get you started:

http://www.hibernate.org/subprojects/validator.html

And also:

http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/html_single/

There are also plenty of tutorials out there too.

like image 127
worldwidejimbo Avatar answered Mar 17 '26 23:03

worldwidejimbo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!