Web Services Tutorial
Web Services tutorial is designed for beginners and professionals providing basic and
advanced concepts of web services such as protocols, SOAP, RESTful, java web service
implementation, JAX-WS and JAX-RS tutorials and examples.
Web service is a technology to communicate one programming language with another.
For example, java programming language can interact with PHP and .Net by using web
services. In other words, web service provides a way to achieve interoperability.
Java Web Services Tutorial
In this tutorial, you will be able to learn java web services and its specifications such as
JAX-WS and JAX-RS.
There are two ways to write the code for JAX-WS by RPC style and Document style. Like
JAX-WS, JAX-RS can be written by Jersey and RESTeasy. We will learn all these
technologies later.
Web Services Examples
In this tutorial, we will see a lot of web services examples like JAX-WS example by RPC
style, JAX-WS example by document style, JAX-RS example by Jersey and JAX-RS
example by RESTeasy implementation.
Problem
Web services tutorial is developed and tested carefully. But if you find any problem or
mistake in our tutorial, you can report to us. We assure, you will not find any problem in
web services tutorial.
What is Web Service
A Web Service is can be defined by following ways:
is a client server application or application component for communication.
method of communication between two devices over network.
is a software system for interoperable machine to machine communication.
is a collection of standards or protocols for exchanging information between two
devices or application.
Let's understand it by the figure given below:
As you can see in the figure, java, .net or PHP applications can communicate with other
applications through web service over the network. For example, java application can
interact with Java, .Net and PHP applications. So web service is a language independent
way of communication.
Types of Web Services
There are mainly two types of web services.
1. SOAP web services.
2. RESTful web services.
Web Service Components
There are three major web service components.
1. SOAP
2. WSDL
3. UDDI
SOAP
SOAP is an acronym for Simple Object Access Protocol.
SOAP is a XML-based protocol for accessing web services.
SOAP is a W3C recommendation for communication between applications.
SOAP is XML based, so it is platform independent and language independent. In other
words, it can be used with Java, .Net or PHP language on any platform.
WSDL
WSDL is an acronym for Web Services Description Language.
WSDL is a xml document containing information about web services such as method
name, method parameter and how to access it.
WSDL is a part of UDDI. It acts as a interface between web service applications.
WSDL is pronounced as wiz-dull.
UDDI
UDDI is an acronym for Universal Description, Discovery and Integration.
UDDI is a XML based framework for describing, discovering and integrating web services.
UDDI is a directory of web service interfaces described by WSDL, containing information
about web services.
SOAP Web Services
SOAP stands for Simple Object Access Protocol. It is a XML-based protocol for accessing
web services.
SOAP is a W3C recommendation for communication between two applications.
SOAP is XML based protocol. It is platform independent and language independent. By
using SOAP, you will be able to interact with other programming language applications.
Advantages of Soap Web Services
WS Security: SOAP defines its own security known as WS Security.
Language and Platform independent: SOAP web services can be written in any
programming language and executed in any platform.
Disadvantages of Soap Web Services
Slow: SOAP uses XML format that must be parsed to be read. It defines many standards
that must be followed while developing the SOAP applications. So it is slow and
consumes more bandwidth and resource.
WSDL dependent: SOAP uses WSDL and doesn't have any other mechanism to
discover the service.
RESTful Web Services
REST stands for REpresentational State Transfer.
REST is an architectural style not a protocol.
Advantages of RESTful Web Services
Fast: RESTful Web Services are fast because there is no strict specification like SOAP. It
consumes less bandwidth and resource.
Language and Platform independent: RESTful web services can be written in any
programming language and executed in any platform.
Can use SOAP: RESTful web services can use SOAP web services as the
implementation.
Permits different data format: RESTful web service permits different data format
such as Plain Text, HTML, XML and JSON.
SOAP vs REST Web Services
There are many differences between SOAP and REST web services. The important 10
differences between SOAP and REST are given below:
No.
SOAP
REST
1)
SOAP is a protocol.
REST is an architectural style.
2)
SOAP stands for Simple Object Access
Protocol.
REST stands
for REpresentational State
Transfer.
3)
SOAP can't use REST because it is a
protocol.
REST can use SOAP web
services because it is a concept
and can use any protocol like
HTTP, SOAP.
4)
SOAP uses services interfaces to
expose the business logic.
REST uses URI to expose
business logic.
5)
JAX-WS is the java API for SOAP web
services.
JAX-RS is the java API for
RESTful web services.
6)
SOAP defines standards to be strictly
followed.
REST does not define too much
standards like SOAP.
7)
SOAP requires more bandwidth and
resource than REST.
REST requires less
bandwidth and resource than
SOAP.
8)
SOAP defines its own security.
RESTful web services inherits
security measures from the
underlying transport.
9)
SOAP permits XML data format only.
REST permits different data
format such as Plain text, HTML,
XML, JSON etc.
10)
SOAP is less preferred than REST.
REST more preferred than
SOAP.
Service Oriented Architecture (SOA)
Service Oriented Architecture or SOA is a design pattern. It is designed to provide
services to other applications through protocol. It is a concept only and not tied to any
programming language or platform.
Web services is a technology of SOA most likely.
Service
A service is well-defined, self-contained function that represents unit of functionality. A
service can exchange information from another service. It is not dependent on the state
of another service.
Service Connections
The figure given below illustrates the service oriented architecture. Service consumer
sends service request to the service provider and service provider sends the service
response to the service consumer. The service connection is understandable to both
service consumer and service provider.
Java Web Services Tutorial
Java web services tutorial provides concepts and examples of two main java web
services api: JAX-WS and JAX-RS. The java web service application can be accessed by
other programming languages such as .Net and PHP.
Java web service application perform communication through WSDL (Web Services
Description Language). There are two ways to write java web service application code:
SOAP and RESTful.
Java Web Services API
There are two main API's defined by Java for developing web service applications since
JavaEE 6.
1) JAX-WS: for SOAP web services. The are two ways to write JAX-WS application code:
by RPC style and Document style.
2) JAX-RS: for RESTful web services. There are mainly 2 implementation currently in
use for creating JAX-RS application: Jersey and RESTeasy.
We will see full tutorials of JAX-WS and JAX-RS in next page.
JAX-WS Tutorial
JAX-WS tutorial is provides concepts and examples of JAX-WS API. This JAX-WS
tutorial is designed for beginners and professionals.
There are two ways to develop JAX-WS example.
1. RPC style
2. Document style
Difference between RPC vs Document style web
services
Difference between RPC and Document web
services
There are many differences between RPC and Document web services. The important
differences between RPC and Document are given below:
RPC Style
1) RPC style web services use method name and parameters to generate XML structure.
2) The generated WSDL is difficult to be validated against schema.
3) In RPC style, SOAP message is sent as many elements.
4) RPC style message is tightly coupled.
5) In RPC style, SOAP message keeps the operation name.
6) In RPC style, parameters are sent as discrete values.
Let's see the RPC style generated WSDL file.
WSDL file:
In WSDL file, it doesn't specify the types details.
1. <types/>
For message part, it defines name and type attributes.
1. <message name="getHelloWorldAsString">
2. <part name="arg0" type="xsd:string"/>
3. </message>
4. <message name="getHelloWorldAsStringResponse">
5. <part name="return" type="xsd:string"/>
6. </message>
For soap:body, it defines use and namespace attributes.
1. <binding name="HelloWorldImplPortBinding" type="tns:HelloWorld">
2. <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
3. <operation name="getHelloWorldAsString">
4. <soap:operation soapAction=""/>
5. <input>
6. <soap:body use="literal" namespace="http://javatpoint.com/"/>
7. </input>
8. <output>
9. <soap:body use="literal" namespace="http://javatpoint.com/"/>
10. </output>
11. </operation>
12. </binding>
Document Style
1) Document style web services can be validated against predefined schema.
2) In document style, SOAP message is sent as a single document.
3) Document style message is loosely coupled.
4) In Document style, SOAP message loses the operation name.
5) In Document style, parameters are sent in XML format.
Let's see the Document style generated WSDL file.
WSDL file:
In WSDL file, it specifies types details having namespace and schemaLocation.
1. <types>
2. <xsd:schema>
3. <xsd:import namespace="http://javatpoint.com/" schemaLocation="http://local
host:7779/ws/hello?xsd=1"/>
4. </xsd:schema>
5. </types>
For message part, it defines name and element attributes.
1. <message name="getHelloWorldAsString">
2. <part name="parameters" element="tns:getHelloWorldAsString"/>
3. </message>
4. <message name="getHelloWorldAsStringResponse">
5. <part name="parameters" element="tns:getHelloWorldAsStringResponse"/>
6. </message>
For soap:body, it defines use attribute only not namespace.
1. <binding name="HelloWorldImplPortBinding" type="tns:HelloWorld">
2. <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="docum
ent"/>
3. <operation name="getHelloWorldAsString">
4. <soap:operation soapAction=""/>
5. <input>
6. <soap:body use="literal"/>
7. </input>
8. <output>
9. <soap:body use="literal"/>
10. </output>
11. </operation>
12. </binding>
JAX-WS Example RPC Style
JAX-WS Example RPC Style
Creating JAX-WS example is a easy task because it requires no extra configuration
settings.
JAX-WS API is inbuilt in JDK, so you don't need to load any extra jar file for it. Let's see
a simple example of JAX-WS example in RPC style.
There are created 4 files for hello world JAX-WS example:
1. HelloWorld.java
2. HelloWorldImpl.java
3. Publisher.java
4. HelloWorldClient.java
The first 3 files are created for server side and 1 application for client side.
JAX-WS Server Code
File: HelloWorld.java
1. package com.javatpoint;
2. import javax.jws.WebMethod;
3. import javax.jws.WebService;
4. import javax.jws.soap.SOAPBinding;
5. import javax.jws.soap.SOAPBinding.Style;
6. //Service Endpoint Interface
7. @WebService
8. @SOAPBinding(style = Style.RPC)
9. public interface HelloWorld{
10. @WebMethod String getHelloWorldAsString(String name);
11. }
File: HelloWorldImpl.java
1. package com.javatpoint;
2. import javax.jws.WebService;
3. //Service Implementation
4. @WebService(endpointInterface = "com.javatpoint.HelloWorld")
5. public class HelloWorldImpl implements HelloWorld{
6. @Override
7. public String getHelloWorldAsString(String name) {
8. return "Hello World JAX-WS " + name;
9. }
10. }
File: Publisher.java
1. package com.javatpoint;
2. import javax.xml.ws.Endpoint;
3. //Endpoint publisher
4. public class HelloWorldPublisher{
5. public static void main(String[] args) {
6. Endpoint.publish("http://localhost:7779/ws/hello", new HelloWorldImpl());
7. }
8. }
How to view generated WSDL
After running the publisher code, you can see the generated WSDL file by visiting the
URL:
1. http://localhost:7779/ws/hello?wsdl
JAX-WS Client Code
File: HelloWorldClient.java
1. package com.javatpoint;
2. import java.net.URL;
3. import javax.xml.namespace.QName;
4. import javax.xml.ws.Service;
5. public class HelloWorldClient{
6. public static void main(String[] args) throws Exception {
7. URL url = new URL("http://localhost:7779/ws/hello?wsdl");
8.
9. //1st argument service URI, refer to wsdl document above
10. //2nd argument is service name, refer to wsdl document above
11. QName qname = new QName("http://javatpoint.com/", "HelloWorldImplSe
rvice");
12. Service service = Service.create(url, qname);
13. HelloWorld hello = service.getPort(HelloWorld.class);
14. System.out.println(hello.getHelloWorldAsString("javatpoint rpc"));
15. }
16. }
Output:
Hello World JAX-WS javatpoint rpc
JAX-WS Example Document Style
Click me to see JAX-WS Example Document Style
JAX-WS Example Document Style
Like RPC style, we can create JAX-WS example in document style. To do so, you need to
change only one line in service interface.
You need to use Style.DOCUMENT for @SOAPBinding annotation in place of Style.RPC.
Let's have a quick look at this:
File: HelloWorld.java
1. @SOAPBinding(style = Style.DOCUMENT)//It is changed from RPC to DOCUMENT
Now Let's see the full example of JAX-WS in document style.
There are created 4 files for hello world JAX-WS document style example:
1. HelloWorld.java
2. HelloWorldImpl.java
3. Publisher.java
4. HelloWorldClient.java
The first 3 files are created for server side and 1 application for client side.
JAX-WS Server Code
File: HelloWorld.java
1. package com.javatpoint;
2. import javax.jws.WebMethod;
3. import javax.jws.WebService;
4. import javax.jws.soap.SOAPBinding;
5. import javax.jws.soap.SOAPBinding.Style;
6. //Service Endpoint Interface
7. @WebService
8. @SOAPBinding(style = Style.DOCUMENT)
9. public interface HelloWorld{
10. @WebMethod String getHelloWorldAsString(String name);
11. }
File: HelloWorldImpl.java
1. package com.javatpoint;
2. import javax.jws.WebService;
3. //Service Implementation
4. @WebService(endpointInterface = "com.javatpoint.HelloWorld")
5. public class HelloWorldImpl implements HelloWorld{
6. @Override
7. public String getHelloWorldAsString(String name) {
8. return "Hello World JAX-WS " + name;
9. }
10. }
File: Publisher.java
1. package com.javatpoint;
2. import javax.xml.ws.Endpoint;
3. //Endpoint publisher
4. public class HelloWorldPublisher{
5. public static void main(String[] args) {
6. Endpoint.publish("http://localhost:7779/ws/hello", new HelloWorldImpl());
7. }
8. }
How to view generated WSDL
After running the publisher code, you can see the generated WSDL file by visiting the
URL:
1. http://localhost:7779/ws/hello?wsdl
JAX-WS Client Code
File: HelloWorldClient.java
1. package com.javatpoint;
2. import java.net.URL;
3. import javax.xml.namespace.QName;
4. import javax.xml.ws.Service;
5. public class HelloWorldClient{
6. public static void main(String[] args) throws Exception {
7. URL url = new URL("http://localhost:7779/ws/hello?wsdl");
8.
9. //1st argument service URI, refer to wsdl document above
10. //2nd argument is service name, refer to wsdl document above
11. QName qname = new QName("http://javatpoint.com/", "HelloWorldImplSe
rvice");
12. Service service = Service.create(url, qname);
13. HelloWorld hello = service.getPort(HelloWorld.class);
14. System.out.println(hello.getHelloWorldAsString("javatpoint document"));
15. }
16. }
Output:
Hello World JAX-WS javatpoint document