Thursday, August 31, 2017

Difference between SOAP and RESTful Web Service in Java

Though both SOAP and RESTful web services allow a client to query the server for some information, the way they are implemented and used is quite different. The main difference between SOAP and REST is that former provides a standard of communication between client, server and other parties and has restricted a set of rules and format, while REST leverages the ubiquity of HTTP protocol, in both client and servers, to allow them to communicate with each other regardless of their implementation. In short, getting data from a RESTful web service requires less headache than getting data from a SOAP web service.


Since everybody is familiar with HTTP requests like the GET or POST, it's easy to understand and correlated how RESTful web services are working and which URL of REST web service provides what kind of information.

In SOAP, you need to understand lengthy WSDL documents to find out the right methods and the right way to call them.

For example, suppose you want to retrieve today's weather for a particular city from a server that is providing weather information, your RESTful URL will look something like http://weatherdata.org/data/weather/uk/london, which is very similar to HTTP request like http://weatherdata.org/data/weather?q=uk,London.

On the other hand, in order to get the same data using SOAP, you need to create an XML message with the header and body and send it

http://www.webservicex.net/globalweather.asmx?op=GetWeather.

In short, RESTfull web services are much simpler, flexible, and expressive than SOAP web services in Java.

And, 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 on Udemy. It's a great hands-on course to learn how to develop a RESTful web service in Java.



Apart from the obvious, let's find out some more differences between SOAP vs RESTFul web services.



Difference between REST and SOAP in Java

Here are some fundamental differences between REST, RESTful and SOAP Web Services, which will help you not only to understand these two key technologies better but also to answer some tricky Java web services questions based upon these two technologies :



1. Short Form

REST stands for REpresntational State Transfer (REST) while SOAP Stands for Simple Object Access Protocol (SOAP).


2. Architecture style vs Protocol

REST is an architectural style, on which RESTFul web services are built while SOAP is a standard devised to streamline communication between client and server in terms of format, structure, and method.


3. Use of HTTP Protocol

REST takes full advantage of the HTTP protocol, including methods e.g. GET, POST, PUT, and DELETE to represent action like from an application which provides data related to books, GET request can be used to retrieve books, POST can be used to upload data of a new book, and DELETE can be used to remove a book from the library. On the other hand, SOAP uses XML messages to communicate with the server.


4. Supported Format

RESTful web service can return the response in various formats like JSON, XML, and HTML while using SOAP web service you tie your response with XML because the actual response is bundled inside a SOAP message which is always in XML format.


5. Speed

Processing a RESTful web service request is much faster than processing a SOAP message because you need to less parsing. Because of this reason RESTful, web services are faster than SOAP web service.


6. Bandwidth

SOAP messages consume more bandwidth than RESTFul messages for the same type of an operation because XML is more verbose than JSON, the standard way to send RESTFul messages and SOAP has an additional header for every message while RESTFul services utilize HTTP header.


7. Transport Independence

Since SOAP messages are wrapped in a SOAP envelope it can be sent over to any transport mechanism like TCP, FTP, SMTP, or any other protocol. On the other hand, RESTful Web services are heavily dependent upon HTTP protocol.

They used HTTP commands their operation and depends upon on HTTP for transmitting content to the server. Though in the real-world, SOAP is mostly over HTTP so this advantage of transport independence is not really utilized.


8. Resource Identification

RESTful web services utilize URL to identify the desired resources to be accessed while SOAP uses XML messages to identify the desired web procedure or resource to be invoked.


9. Security

Security in RESTful web service can be implemented using standard and traditional solutions for authorized access to certain web resources. While to implement security in SOAP-based web services you need additional infrastructure on the web to enable message or transport-level security concerns.


10. Caching

RESTful web service takes full advantage of the web caching mechanism because they are basically URL based. On the other hand, SOAP web services totally ignore web caching mechanism.


11. Approach

In REST-based web-services, every entity is centered around resources, while in the case of SOAP web service, every entity is centered around interfaces and messages.


12. An Example

In the first paragraph, we have seen one example of requesting the same web service using both SOAP and RESTFul style, you can see that the REST web service is easy to understand, can be cached, and required little effort to understand as compared to SOAP.



Summary - RESTful vs SOAP Web Service

Here is a nice summary of key differences between REST and SOAP style web services :



Some Technical details about SOAP Web Services

1) SOAP stands for Simple Object Access Protocol but nothing is so simple about it :)

2) It's only possible to send XML messages to the server using SOAP web service because your request is embedded inside SOAP envelope which is in XML format, on the other hand, RESTful web services allow you to send a request in various formats e.g. JSON and flexible enough to send JSON responses.



Why REST is better than SOAP?

Now that you know some differences between REST and SOAP web services, let's summarize our reasons for why REST is a better choice for modern-day web service requirement :

1. REST can be consumed by any client like Java, C++, Python client, and even a web browser with Ajax and JavaScript.

2. REST is lightweight as compared to SOAP, it doesn't require CPU consuming XML parsing and it also consumes less bandwidth because unlike SOAP, REST doesn't require a SOAP header for every message.

3. SOAP is an old technology, all modern technical giants are using REST-like Google, Twitter, and Flickr.

4. REST is easy to learn, its just nouns and verbs. If you already know HTTP methods then it's even easier.

5. Java has excellent support for RESTFul web services, well it also has good support for SOAP web services but you have lots of choices here like Jersey, RESTLet, etc.


That's all about the difference between REST and SOAP Web Service in Java. Its's one of the most frequently asked questions on Java web service topic. Since REST is the technology that is right now dominating web service space, it's also important to know the pros and cons REST style of web service provides over good old secure SOAP web services.

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.

15 comments :

Anonymous said...

What is ment by architectural style?
How to implement security in REST?

javin paul said...

@Anonymous, Architectural means aligning the resources so that they can be accessed and fits together with HTTP methods e.g. with GET request following URL will return weather data for London :
http://weatherdata.org/data/weather/uk/london

with POST you can change the data, with DELETE you can delete it etc.

Anonymous said...

Hi javin,
Is REST supports only HTTP protocol?
If it supports other then how should we use it?
How do we implement security for Restful services?

Unknown said...

While SOAP supports SSL (just like REST) it also supports WS-Security which adds some enterprise security features. Supports identity through intermediaries, not just point to point (SSL).
In point-to-point situations confidentiality and data integrity can also be enforced on Web services through the use of Transport Layer Security (TLS), for example, by sending messages over https.
WS-Security however addresses the wider problem of maintaining integrity and confidentiality of messages until after a message was sent from the originating node, providing so called end to end security.
• HTTPS is a transport layer (point-to-point) security mechanism
• WS-Security is an application layer (end-to-end) security mechanism

super_d said...

All difference is just our imagination. There are only two different things messaging and response/request. SOAP and REST are response/request.

Unknown said...

please post how can we differentiate web service and socket programming.
when we use web service over socket vice-versa.
ASAP
(Thanks)

Unknown said...

great

Anonymous said...

you saved my day, thank you so much <3 <3

sanjay said...

Nice

MANISH DASS said...

Nice Explaination

Unknown said...

Thnks for this

Anonymous said...

Hi javin,

Your tutorials are really very nice.The content is very useful.Please post tutorial on IBM websphere application server Steps for installing application,datasource,certifcates,queues and all the steps.

Thanks

javin paul said...

Hello @Annonymous, sure, I'll try to write tutorials on IBM websphere application servers, thanks for suggesting topic.

CorDharel said...

So the real question actually is: Since REST has so many benefits why do still so many companies use SOAP calls? You get the feeling that after reading this article you must be crazy to use SOAP over REST...

sushil said...

Can u please write an article for communication between rest webservice s and soap webservice s

Post a Comment