Introduction to Web Services in Java

Web services are services exposed over a network and is designed to be used automatically by other applications without human interaction.

A practical example for web services is a news web site which also shows cricket score. The cricket score service can be provided as a web service on a different server and our news web site can automatically invoke that service without any human intervention, get the result and update it along with the news. Note that this can also be done using Ajax.

Web services communicate using standard, vendor-independent protocols such as HTTP, XML, Javascript etc. Hence a web service written in one programming language can be invoked by another application written in a different programming language. Web services can be wrapped around existing legacy systems to make them network enabled. Simple web services can be aggregated to more complex ones by calling lower layer web services from a web service implementation, or by chaining together web services to perform higher level business function.  

Web services are different from web applications. While web applications are applications with web pages that reside on a server, primarily for use by humans, a web service is a server-based application component meant primarily for interaction with other programs. A web application might use a web service to retrieve and populate some data from another server without the end user knowing about it. 

 

Types of Web Service

WebServices can be roughly divided into two types based on the architecture they follow:

  1. SOAP-based web services

  2. Restful web services

 

SOAP Based web services

  1. SOAP-based web services were the original kind of web services

  2. They follow RPC-style

    • An RPC-style web service accepts an envelope full of data from its client, and sends a similar envelope back.

    • The method and the scoping information are kept inside the envelope. HTTP is a popular envelope format.

    • SOAP is another popular envelope format (transmitting a SOAP document over HTTP puts the SOAP envelope inside an HTTP envelope).

  3. SOAP Services can be located and invoked by client applications over a network (e.g. internet) automatically without human interaction.

    • To make the automatic locating and invocation possible SOAP web services were designed as self describing through the use of a message protocol called SOAP, service definition called WSDL and a way to locate other web services using UDDI

    • SOAP web services are self-describing and self-contained, as the definition of the message format (WSDL and/or XSD) travels with the message or is accessible to the client, and the server and client requires only minimal software - an HTTP server and/or a SOAP server on the server side and on the client side no additional s/w is required. 

  4. SOAP is based on rules whereas REST is based on guidelines.

    • SOAP web service defines a new vocabularies, standards and protocols.

  5. In SOAP based webservices messages (requests and responses) are exchanged in an XML format called SOAP. 

    • SOAP originally stood for Simple Object Access Protocol, but now some also call it as Service Oriented Architecture (SOA) protocol. 

  6. Though HTTP is the most commonly used protocol with SOAP-based webservices, it is not limited to just HTTP, but can also use other protocols like SMTP, JMS etc

    • A SOAP-based service delivered over HTTP can be considered as a special case of a REST-style service.

  7. In SOAP, the endpoint url will be same for a service that represent a set of operations and different operations are requested using specifics in the request body.

  8. Definition about the service is stored in a document called WSDL in case of SOAP. When a client get a wsdl, it will have all information regarding a SOAP web service.

  9. The request-response pattern is supported by both SOAP and REST. SOAP based services also support other message exchange patterns like one way input, solicit response and publish-subscribe.

  10. You need a specialized client that can send SOAP requests in case of SOAP.

    • SOAP UI is a popular tool that can do this.

    • You may also write a Java client using JAX-WS on your own.

 

RESTful web services

  1. Restful web services came later and is more popular now

  2. They follow a resource oriented way. Every resource will be reprecented by an URI and its current state of representation is transferred using formats such as XML and JSON.

  3. REST web services are not automatically located using an UDDI registry as in case of SOAP web services.

  4. SOAP is based on rules whereas REST is based on guidelines

    • Restful web services share a standard vocabulary of HTTP methods.

      • Every object in a Restful service responds to the same basic interface. 

  5. In a REST-style service, a client might send a standard HTTP request to a web service and receives an appropriate response in a MIME-TYPE of their choice like XML, Text, JSON, user-defined etc.  

  6. HTTP is the only transport protocol for REST. 

  7. REST usually have a single url for a resource and uses the standard set of http operations like POST, GET, PUT, and DELETE, to do various operations on that resource.

    • Note that REST is based on guidelines and not rules as in the case of SOAP. So there is nothing which will enforce you to use a particular approach in REST, like in the case of SOAP.

  8. There is no formal definitions for REST like the WSDL for SOAP. There are some definition formats like WADL, but they have not become very popular. 

  9. REST support only request-response message exchange pattern.

  10. You can make requests to a RESTful web service using your programming language’s HTTP client library or even from your web browser if it is a GET request.

    • There are also plugins available to most browsers.

 

 

Web services support in Java

  • Java supports both SOAP-based and REST-style web services.

  • Java API for XML Web services (JAX-WS) can be used for SOAP-based as well as REST-style, but more suited for SOAP.

    • The current version of JAVA–WS is 2.x and previous version was called JAX RPC 1.x.

    • JAX–WS is commonly shortened to JWS for Java Web Services.

    • JAX–WS was initially seperate, but was then bundled into the Metro web services pack.

      • Metro is part of java 6 and above and hence JAX-WS web services can be compiled and deployed in java 6 and above without any additional software.

      • The downloaded Metro release is a way to do JAX–WS under core java 5.

        • You can download metro from metro.java.net and just JAX-WS from jax-ws.java.net.

      • JAX-WS alone supports only SSL (it supports WS-I BSP).

        • Hence if you want end-to-end message-level security then you need the WSIT/Tango layer of Metro.

        • In addition to JAX-WS RI, metro contains another layer, WSIT/Tango which provides WS-*: Security, SecureConversation, Trust, ReliableMessaging, AtomicTransactions and MEX.

    • JAX–WS require at least core java 5 because of the support of annotation.

  • Java API for XML – Restful web services (JAX–RS) is the standard specification for REST web services.

    • Jersy is a popular JAX-RS implementation.

    • Java defines standard REST support via JAX-RS (The Java API for RESTful Web Services) in JSR 311.

    • You can download JAX-RS from Jersy Homepage at jersey.java.net.

    • Jersey is the open source, production quality, JAX-RS (JSR 311) Reference Implementation for building RESTful Web services.

      • In addition to the Reference Implementation, Jersey also provides an API so that developers may extend Jersey to suit their needs. 

    • Jersy is only one of the implementations for it. There are also other implementations like RESTEasy which may also be used.

      • Since all these implementations are based on the JAX-RS API, you can easily switch to another implementation anytime.

References and notes: 

Message Exchange Patterns (MEP) is a pattern for the exchange of messages between two communicating parties like a server and a client. The most common such pattern is the request-response pattern where the client sends a request and the server sends a response back. For more details on MEPs read message-exchange-patterns-in-web-services

Comments

Hi Hearin,

I am totally new to webservices. I would like to have more clarification on why webservvices are self locating, what it really means?....whether this is possible through the wsdl or UDDI?....so only soap based webservices are self locating?....Could you please explain this more?

 

Thanks in advance,

Chinnu

 

Was it useful?

Self locating and UDDI are not very popular in real world. Even most SOAP projects will have an interface team who create the wsdl and share it in some common repository and both server and client teams can take it and create stub/skelton. 

Coming to REST, you can use UDDI and some registries are supporting it too, but is not very popular. Once you have the initial url you may use links (refer to HATEOAS) to traverse through more end points.

I could see some useful links:

Was it useful?

Thanks heartin for the quick reply...now I got the idea

Was it useful?

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (53) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (8) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (12) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)