Saturday, May 13, 2023

Mapping of HTTP Methods to RESTful Web Services Function in Java?

RESTful web services heavily rely on HTTP by design. They use different HTTP methods to perform their job and uses an HTTP response code to inform clients about the success or failure of a particular request. REST stands for Representational State transfer and it uses HTTP to allow two systems to communicate via remote calls. RESTFul web services are a collection of REST URI which points to resources. These URI can point to a single resource or a collection of resources. For example, you would expect /employee/101 to contain details of employees with 101 and /employees to return a list of all employees. In the RESTFul web service, HTTP request types signify the action to take for the resource.

For example, by using an HTTP GET request on /employee/101, you can retrieve the details of that user.

By using POST on employe/102 would create a new user with employee id 102, PUT request type on /employee/101 can be used to update details of employee with id 101, and DELETE method on /employee/101 can be used to remove data for that id.

By the way, in the case of PUT and POST method representation would be in the request body. This is also a very popular question on Java and Spring Boot interviews where you need to work on RESTful Web service and knowing what does each HTTP method does is key to write better REST APIs.

If you are a complete beginner into RESTful web services then I also suggest you take a look at the Master Java Web Service and RESTful API with Spring Boot course by Ranga Rao of In28Minutes on Udemy. It's a great hands-on course to learn how to develop RESTful web service in Java.




Purpose of HTTP Requests in RESTful WebService

The purpose of each of the HTTP request types, when used with a RESTful web service, is as follows:

1. GET

Retrieves data from the server (should only retrieve data and should have no other effect). This is also called an idempotent method. here is an example of a GET request to retrieve the book with id 123 from the Server.
GET /books/123


2. POST

This HTTP request type is usually used for creating an entity i.e. a resource without an id. Once the request is successfully created, an id of the newly created entity is returned as part of the response to this HTTP request. It is often used when uploading a file or submitting a completed web form.

For example, the following URL will create  a new book in the server
POST /books/


3. PUT

Similar to POST, but used to update an existing entity. You pass the id of the existing resource along with the PUT request type. For example, the following URL will replace the book with id 123 in the server
PUT /books/123

If you want to learn more details on when to use PUT and POST while creating RESTful API., you can further see RESTful Web Services, Java, Spring Boot, Spring MVC, and JPA course by Sergey Kargopolov on Udemy. It's a great course to learn RESTful applications using Spring Boot and Java. 




4. DELETE

Removes the resource from the server. Similar to PUT you need to pass the id of the resource to be deleted. For example, the following URL will remove the book with id 123 in the server

DELETE /books/123


5. TRACE

Provides a means to test what a machine along the network path receives when a request is made. As such, it simply returns what was sent.



6. HEAD
Same as the GET method for a resource, but returns only the response headers (i.e., with no entity-body). Similar to the GET request, HEAD Is also idempotent i.e. no side effect on Server.
See  REST in Practice: Hypermedia and Systems Architecture by Jim Webber, Savas Parastatidis, and Ian Robinson for more details on how to use different HTTP methods while designing RESTful API.



7. OPTIONS

It allows a client to request information about the request methods supported by a service. The relevant response header is Allow and it simply lists the supported methods. (It can also be used to request information about the request methods supported for the server where the service resides by using a * wildcard in the URI.)


8. CONNECT

Primarily used to establish a network connection to a resource (usually via some proxy that can be requested to forward an HTTP request as TCP and maintain the connection). Once established, the response sends a 200 status code and a “Connection Established” message.

You can also read RESTful Web Services by Leonard Richardson, Sam Ruby, and David Heinemeier Hansson to learn RESTful web service in depth. One of the best books to learn the fundamentals of RESTful WebService.

Here is a nice diagram showing the usage of different properties of HTTP methods in RESTFul Web Service:




That's all about the purpose of different HTTP request types in the RESTFul Web service, be it in Java or any other technology. Most important is to understand the difference between PUT and POST  request types, though both PUT and POST can be used to create and update an entity, POST is usually preferred for creating, and PUT is preferred for updating an existing entity.


Further Learning
REST Java Web Services
Master Java Web Service and RESTful API with Spring Boot
RESTFul Services in Java using Jersey By Bryan Hansen


 Other RESTful WebService articles you may like to explore
  • 5 Best Courses to learn RESTful Web Service (best courses)
  • Difference between SOAP and RESTful web service in Java? (see here)
  • How to consume JSON from REST API in Java (example)
  • My favorite books to learn RESTful Web Service (books)
  • Restlet HelloWorld Example in Java and Eclipse (tutorial)
  • 5 Books to prepare Java EE interviews (list)
  • 15 REST Web Services framework Interview Question (see here)
  • What are idempotent and safe methods of HTTP and REST? (answer)
  • Top 10 RESTful web services interview questions for Java developers (see)
  • What is the purpose of different HTTP methods in REST? (answer)
  • Top 10 Java Web service interview questions (see here)
  • When to use the PUT vs POST method in REST? (answer)
  • 3 ways to convert String to JSON Object in Java? (example)
  • 5 JSON parsing library Java and JEE developer should know (article)
  • 20+ Spring and REST Interview Questions (answers)

Thanks for reading this article so far. If you find my explanation of different HTTP methods and their use in RESTP API then please share this with your friends and colleagues. If you have any questions or feedback then please drop a note. 

P.S. - If you want to learn how to develop RESTful Web Services using Spring Framework, check out Eugen Paraschiv's REST with Spring course. He has recently launched the certification version of the course, which is full of exercises and examples to further cement the real-world concepts you will learn from the course.

5 comments :

Vinod Kumar Kashyap said...

Hey,

Fan of your blog.
A little mistake in paragraph 3.
POST for saving and PUT for updating. But you have mentioned opposite.

Thanks
Vinod

javin paul said...

Hello Vinod, thanks for pointing it out. Yes, POST is for creating and PUT is for updating, corrected it now.

asm0dey said...

PATCH?

daltwisney said...

Where is PATCH?

javin paul said...

@daltwisney, Sorry, I forgot to add PATCH, thanks for pointing it out, will add it as well. Btw, PATCH can be used to update partial resources. For instance, when you only need to update one field of the resource, using PUT to update a complete resource representation might be cumbersome and utilizes more bandwidth, using PATH is better. Also, its worth remember that PATCH is neither idempotent nor safe.

Post a Comment