Using Restful approach with SpringMVC
In this project we used the Rest principles. Let us talk about those principles.
The first principle that we will talk about is of a friendly URl. In this project will not be displayed an ugly URL like: uaicontacs.com/?myFirst=1&mySecond=oliver%20%tree. To have friendly URLs in the project helps the user in two ways: 1) the user can add the link in the bookmark. If the request uses the HTTP method not equals to GET this would not be possible; 2) makes easier to a user to send the URL to a friend. e.g.: a friendly URL would look like: “myStore/products/uaiTv33”.
Other Rest principle is that the HTTP requests will use the HTTP methods like GET, PUT, POST, DELETE, etc. These methods were invoked with the Angular.js framework that is responsible to invoke the controller. If you do not know how Angular.js works, these Ajax calls could be done with JQuery (the syntax is very similar).
Some HTTP methods are not support by the most of the know browsers, usually the browsers supports only GET and POST inside a form: <form method=”POST”>. If you add other kind of HTTP method usually the browser will replace it by a supported HTTP method (GET or POST).
Each method has its specific purposes:
GET: used to get values; it could be used like GET “uaicontacts/contacts” that will return a list of contacts. If the method is to get data of a specific contact the URL would be something like GET “uaicontacts/contacts/1”. Imagine now that you want to list all phone numbers of a contact, the URL would be like GET “uaicontacts/contacts/1/phones”. Notice that the method GET has no body and all parameters will be visible in the URL like a PATH (in the sample that we just saw) or with parameters like: “?name=uaiHebert”.
POST: used to insert new data in the project, e.g.: a new contact to be created in the database. This HTTP method may come with a body; a body means that the data has been wrapped in the <form> html tag.
PUT: this HTTP method is used to update data in the project, e.g.: edit a contact name. The data will be in the body of the request, but the URL will indicate which resource will be edited PUT “uaicontacts/contacts/1”. The HTTP PUT method could be used to create some data that does not exist in the database yet – works like the Hibernate saveOrUpdate method.
DELETE: HTTP method that is used to delete data from the project; the URL will contains the resource ID to be deleted “uaicontacts/contacts/1” and it does not support a formulary “<form…>”.
The Rest SpringMVC integration it is easy to use. The available HTTP methods can be found in the RequestMethod enum and used in the methods like this: @RequestMapping(method = RequestMethod.POST).
Every HTTP request that your project will handle must be answered with a response status number. With Spring MVC it is easy to do, e.g.: return new ResponseEntity<String>(“success”, HttpStatus.OK);.The HttpStatus.OK has a number value of 200. There are others codes that can be used to represent different kind of response status code. The enum HttpStatus has several other available status.
Take a look below if you want to understand more about response status code: