About Alexey Zvolinskiy

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 MVC: REST application with CNVR vol. 2

In the previous post I have made a fast overview of the setting up environment for Spring MVC REST project with CNVR. In this part I can focus directly on a controller and on demonstration of a REST service. As usually I’m going to make a short introduction and after that I will go through the controller methods and explain all essential moments.

Since further I will talk about the REST service, I need to say some sentences about the basic REST concepts. You probably heard before about sites which provide API to use their functionality. This becomes possible with the help of REST or SOAP, but in this article I will talk about the REST.

E.g. you want to develop an application for a university library which will work with students and books. You can implement all controllers using REST. This decision will make your application opened for a collaboration with other applications which can use the application’s API. For the more information about REST features you need to visit special sites.

Spring MVC REST controller

The Smartphone application is oriented on HTTP clients such as browsers and on JSON clients. JSON format can be consumed by various types of clients, but it’s doesn’t matter now.

The first method is a simple Spring MVC controller. I have explained multiple times in my previous posts how to work with Spring MVC controllers. But you can notice that the method is unusual, because it contains invocation of the second one method. The second method is a REST method whith standart REST annotations: @ResponseBody and @RequestBody.

When you submit a form with a data about a new smartphone to “../smartphones/create.html” for a processing, content negotiating view resolver determines that you need to receive a html page. In case when you call URL “../smartphones/create.json” you will get back JSON document. Because I specified in the WebAppConfig that CNVR need to make its decision based on URL sufix.

You can ask: So what is the reason of CNVR usage if we still need to create several methods for the same operation? Let’s suppose that the smartphone application has to support 2 extra content types: XML and PDF. In this case CNVR will make our life easier and we won’t develop extra methods, just add appropriate view resolvers in the WebAppConfig. The situation will become ideal if we start use AJAX in the application. That’s mean we can eliminate methods which return ModelAndView objects.

REST: GET ALL RECORDS

In the previous paragraph I have made a detailed overview of the CNVR principles in practice. So now I will just post concrete code snippets which correspond to the appropriate operation.

Summary

I hope this part was clear for you. Definitely you need to have some basic knowledge of Spring and REST in order to fully understand this article. Don’t ignore links which I put in the article for the more information. In the third part I’m going to demonstrate how this application works.

Great Post. I have one question on it. What is the best practice , I am in process of developing an application which can either be consumed by browser or mobile phones, so I have decided to used REST API of Spring which is good enough for Mobile Application as they will be calling REST API and will be getting the JSON Data and use there own logic to show in the data. My problem is that the browser client which I have developing will also going to use same REST API (Using Spring) and getting the JSON Back, now… Read more »

Join Us

With 1,240,600 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!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.