Alexey Zvolinskiy

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. 3

This is a final, third part of Spring MVC REST tutorial with CNVR. Here I will demonstrate how all that stuff works, which I have developed within first two parts. Here will be four sections for each type of CRUD operations: CREATE, READ, UPDATE, DELETE. As a pre-requirement you need to install any REST client for a working with a REST requests.

Spring REST: CREATE

The first operation will be creation of a smartphone. I’m going to use POSTMAN REST client. It’s a Google Chrome extension and you can download it absolutely free. When I perform CREATE or UPDATE operation I have to pass some data in a body of the request which will be used for creation or update of entity. So let’s see how it works:

Spring-REST-Create

On the image above you can see completed CREATE operation. Let’s consider the most essential parts of it.

  • The URL contains .json sufix
  • POST selected as a HTTP method
  • Request body it is the JSON document which contain required fields for a smartphone creation
  • Response body it is the JSON document which in our case equals to Request body

I hope you noticed one more important detail. I talk about the Content-type = application/json. As you remember in the methods of controoler I specified such parameters as “consumes“:

...
	@RequestMapping(value="/create", method=RequestMethod.POST, 
			produces = "application/json", consumes = "application/json")
	@ResponseBody
	public Smartphone createSmartphone(@RequestBody Smartphone smartphone) {
		return smartphoneService.create(smartphone);
	}
...

That’s mean that the method will work just with JSON. Spring permits to set multiple types for “produces” and “consumes” attributes.

Spring REST: READ

The second operation will show how to select some records.

Spring-REST-Read

In this case I just used the HTTP GET request and it returned to me a list of existing smartphones in JSON format.

Spring REST: UPDATE

A HTTP PUT request as a HTTP POST request should have body content to be have information about updating data.

Spring-REST-Update

Spring REST: DELETE

In the most straightforward case HTTP DELETE request just need to know ID of entity which will be removed.

Spring-REST-Delete

As you see everything goes as in previous examples except HTTP request type, which was changed to DELETE.

It’s not all

But as you remember I have developed some methods in the controller which return ModelAndView object. Hence you can do the same operations through a web interface. It was very challenging for me to write these tutorials. I covered here just a top of the “REST” iceberg. In the future I will write some articles about AJAX, exception handling and other cool stuff related to the REST.
 

Reference: Spring MVC: REST application with CNVR vol. 3 from our JCG partner Alexey Zvolinskiy at the Fruzenshtein’s notes blog.
Related Whitepaper:

Introduction to Web Applications Development

Kick start your web apps development with this introductory ebook!

This 376 page eBook 'Introduction to Web Applications Development', starts with an introduction to the internet, including a brief history of the TCT/IP protocol and World Wide Web. It defines the basic concepts for web servers and studies the case of Apache, the most used webserver, while other free software webservers are not forgotten. It continues with webpage design focusing on HTML and JavaScript. XML Schemas, their validation and transformation are covered as well as dynamic webpages built with CGI, PHP or JSP and database access.

Get it Now!  

Leave a Reply


× 9 = twenty seven



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
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.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! 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.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books