SOA vs Web Services

SOA vs Web Services

Service-Oriented Architecture (SOA)  and  Web Services  are often mentioned together, but they are not the same. SOA is a design principle, while Web Services are a technology that can be used to implement SOA.

SOA vs Web Services

Service-Oriented Architecture (SOA)

SOA  is an architectural pattern that allows different services to communicate with each other over a network. These services are independent, loosely coupled, and can be reused across different applications. The main principles of SOA include:

  • 1.  Interoperability : Services can be used by clients on different platforms and technologies.
  • 2.  Loose Coupling : Services are designed to minimize dependencies.
  • 3.  Reusability : Services can be used across different applications.
  • 4.  Abstraction : Services hide the complexity of the underlying implementation.
  • 5.  Composability : Services can be composed to form larger, more complex services.

Web Services

Web Services  are a technology used to implement SOA. They provide a way to make services available over the internet using standard protocols. There are two main types of Web Services:

  • 1.  SOAP Web Services : Use SOAP (Simple Object Access Protocol) for communication. They are highly extensible and support complex operations and security features.
  • 2.  RESTful Web Services : Use HTTP protocols for communication. They are lightweight, scalable, and more straightforward than SOAP-based services.

Key Differences

  • 1.  Scope :
    • SOA : An architectural pattern that can use various protocols and technologies.
    • Web Services : A specific implementation of SOA using web-based protocols.
  • 2.  Communication :
    • SOA : Can use various communication protocols like HTTP, JMS, AMQP, etc.
    • Web Services : Primarily use HTTP/HTTPS for communication.
  • 3.  Implementation :
    • SOA : Can be implemented using various technologies like Web Services, RPC, CORBA, etc.
    • Web Services : Implemented using standards like SOAP, WSDL, UDDI for SOAP Web Services, and HTTP methods for RESTful Web Services.
  • 4.  Protocol Standards :
    • SOA : Not limited to web protocols; can include other protocols.
    • Web Services : Use standard web protocols (HTTP, XML, JSON).

Explanation in Java Example

Let’s consider a simple SOA implementation using Java with both SOAP and RESTful Web Services.

1. Maven Dependencies (pom.xml)
xml
<dependencies>
    <dependency>
        <groupId>javax.xml.ws</groupId>
        <artifactId>jaxws-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>jaxws-rt</artifactId>
        <version>2.3.1</version>
    </dependency>
</dependencies>

2. SOAP Service Endpoint (CalculatorService.java)
java
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class CalculatorService {

    @WebMethod
    public int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        Endpoint.publish("http://localhost:8080/ws/calculator", new CalculatorService());
    }
}

  • 3.  WSDL Generation :
    • Deploy the service and access the WSDL at http://localhost:8080/ws/calculator?wsdl.

RESTful Web Service Example

1. Maven Dependencies (pom.xml)
xml
<dependencies>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.29.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.29.1</version>
    </dependency>
</dependencies>

2. RESTful Service Endpoint (CalculatorResource.java)
java
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/calculator")
public class CalculatorResource {

    @GET
    @Path("/add")
    @Produces(MediaType.APPLICATION_JSON)
    public Response add(@QueryParam("a") int a, @QueryParam("b") int b) {
        int result = a + b;
        return Response.ok(result).build();
    }
}

3. Application Config Class (RestApplication.java)
java
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/api")
public class RestApplication extends Application {
    // No need to implement any methods
}

4. web.xml Configuration
xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <display-name>RestApp</display-name>

    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.example.RestApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

</web-app>

Explanation

  • SOAP Web Service : Demonstrates a simple calculator service with an add method, accessible via a SOAP-based interface.
  • RESTful Web Service : Demonstrates a simple calculator service with an add endpoint, accessible via HTTP GET requests.