From the course: Java EE 8: Web Services

REST and SOAP - Java EE Tutorial

From the course: Java EE 8: Web Services

Start my 1-month free trial

REST and SOAP

- [Narrator] Let's now line up SOAP and REST, side by side for the first time. It's important to understand the differences between these two web service styles and what their respective use cases are. This is a concept that comes up in technical phone screens. I've certainly asked questions based on the concepts I'm about to outline. First thing to know about these two is how they treat URLs. REST for web services are all about the URL, how the URL is structured, its uniqueness and maintainability are integral to the very concept of REST. It's almost standard for a single RESTful web service to host multiple URLs as web resources for clients to access. SOAP, on the other hand, isn't as dependent on the URL. For a single SOAP web service, you're usually going to have a single URL to use to access any number of functions that that service has to offer. Then we talk about the use of protocols. REST is fundamentally built on the HTTP protocol. It's tightly coupled to the semantics of HTTP. So using the get, and post and puts properly are very important to the design and development of RESTful web services. It doesn't have its own message structures as it were, you're expected to use all the pieces of HTTP to implement the client/server relationship. HTTP headers, body, URL, all of it. SOAP, on the other hand, is its own protocol. It contains the syntax, the structure, and rules for communicating between the SOAP client and the SOAP web service. It uses HTTP mostly to transport the message payload back and forth between client and server. Every communication sent in SOAP uses HTTP POST, and maybe a few HTTP headers in some scenarios. Matter of fact, you don't even have to use HTTP, JMS, XMLRPC, and many other communication protocols are a okay with SOAP. This one's a good one: message formats. Interviewing developers I get to hear that RESTful services must use JSON format for communication. Absolutely not, RESTful services can communicate in any format, XML, Protobuf, Avro, whatever. Neither JAX-RS nor REST are bound to any specific message format. Choose the right message format for your use case. SOAP, on the other hand, is decidedly XML, the entire syntax and structure of the protocol is built on XML. So there's not a lot of wiggle room there. When to use which? REST is more of a principle or design pattern, so it tends to be more flexible and lends itself to enterprise architectures where that's preferable. So when we're talking about microservices, mobile applications and the like, REST is usually the approach of choice. SOAP, on the other hand, is a fully-contained protocol that mandates specifics about how the message can be structured and the syntax and use. You have the WSDL, the document that literally dictates what, where, and how you're allowed to consume a given SOAP web service. All this makes SOAP ideal for scenarios where structure and stability is important. When you need guarantees as to how a web service will behave and what's valid input and output and so forth, SOAP is the ideal web service style for that.

Contents