Alexey is a test developer with solid experience in automation of web-applications using Java, TestNG and Selenium. He is so much into QA that even after work he provides training courses for junior QA engineers.

Spring REST: Exception handling vol. 3

It’s a last article from the series about Spring REST exception handling. Finally this time I will talk about handling of REST exceptions which can occur during a form processing. So in this tutorial you will see everything related to REST, forms and exception handling. What about client side? JQuery will be used to reflect all responses of the REST service.

As in the previous tutorials I will work with the Smartphone application. Now is a good time to announce the main aim of this post – a Smartphone entity need to be validated before creation and editing.

Pay your attention on @Length and @Range annotations. These annotation are a standard way for a bean validation. Using of these annotations become possible after updating of already existed pom.xml file by adding to it following dependencies:

Length.smartphone.producer = Length of a Smartphone producer should be from 1 to 20 characters.
Length.smartphone.model = Length of a Smartphone model should be from 1 to 20 characters.
Range.smartphone.price = Price of a Smartphone should be from 1 to 1 500.00 $

Now let’s look on the createSmartphone method in SmartphoneController class:

The @Valid annotation applied to method argument. Pay extra attention on absence of BindingResult, it is not required for validation like in a classic Spring MVC application. If some property in the Smartphone entity will have inappropriate value the @Value annotation will throw MethodArgumentNotValidException.

Handling of exceptions during entity creation / editing requires different model for information representation. I mean ErrorInfo class from the previous article. We need to transfer additional information which will contain error field name and certain error message for that field. For this purpose here is a new class:

The full version of client new-phone.jsp file can be found by link in the start of the article.

This is the end, I have to make a demonstration of all stuff which we developed in the article above. So scenario is simple, I’m going to open New Smartphone page and submit a form with invalid data.

Summary

I hope that my three articles about exception handling in a Spring REST application were useful for you and you have learned something new. These articles highlight just a basic flow of exception handling, all other things you can get only throughout practice on real projects. Thanks for reading of my blog.

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Join Us

With 1,043,221 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!