You are on page 1of 7

WEB SERVICES

Yuvaraj G

Yuvaraj G 1 yuvashan21@gmail.com
Web services are XML-based information exchange systems that use the Internet for direct
application-to-application interaction. Web services are application components which communicate
using open protocols. In simple terms a Web Service is an application or business logic that is
accessible using standard Internet protocols.

How does Web Services work?

Let us assume 2 applications App1(Consumer 1) and App2(Producer2). App13, where the client
resides, requests information or services from App2. App2, in turn, sends the info/services back to
App1, who displays the information to the Client.

Note: Server side implementations of App1(Consumer) & App2(Producer) can exist in the same or
diff. technologies(ie. Java, VB.net, Asp.net with C# code ,between Java and Python ,etc).

Web Services Components


The basic platform for Web services is XML and HTTP. Web services can be discovered using
UDDI - Registration and Lookup (Universal Description, Discovery and Integration).one can publish
our application's functions to everyone.

Web services communicates between two different applications in XML Language.


• XML provides a language which can be used between different platforms and
programming languages and still express complex messages and functions.
• XML stands for EXtensible Markup Language
• XML tags are not predefined. You must define your own tags.

SOAP (Simple Object Access Protocol), REST (Representation State Transfer) are two Web Services
concepts.
SOAP RESTful
1)Traditional More Recent.

2)Protocol Independent (Can interact with SMTP, FTP, Protocol dependent(Only work with HTTP )
etc)

3)Sits on Transport Layer Sits on Application Layer

4)Heavy weight Light weight

5)Difficult to learn/implement Easier to learn/implement. Built-in Cache and Better


performance(The Yahoo! Search Web Services are all REST services)

6)JAX-WS : Need web.xml and sun-jaxws.xml entry JAX-RS : Need only web.xml that also only one entry
for each service . for all services.

WSDL:
As it describes the complete contract for Application Communication. Web services are
widely approachable by using WSDL definitions to generate code(stub) that precisely knows how
to interact with the Web service(App2). It specifies that how web services(App2) are declared,
and the operations (or methods) the service exposes( ie. to know the objects, its methods, accessing
procedures which are required to call from client to Consume). For example, assuming the service
provides a single publicly available method, called sayHello. This method expects a single string
parameter and returns a single string that can be known from WSDL.

Yuvaraj G 2 yuvashan21@gmail.com
Fig 1.1: Web service
Application-to-Application Interaction(in Detail):

On receiving xml as response,To automatically bind XML data to objects (that xml tag value
should be set into the corresponding java class setters) is called as Unmarshalling((See (b) in Fig. 1.2))
, so that can be used further inside App1(Consumer) for process it or databases that store it. Converting
java files(objects) setter values into xml format is known as Marshaling((See (c) in Fig. 1.2)) that can
be done in App2(Producer) when produce the response.

Using JAXB (Java and XML Binding compiler) to generate java(JAXB) classes from
XSD(used as Input) which needed for Unmarshalling. ( See (a) in Fig. 1.2)

Fig 1.2: JAXB Architectural Overview

XSD (XML Schema Definition) is used to define a "data contract" between producers and
consumers of XML data so that each knows what to do and expect and also used to validate that a
specific XML data message meets the contract but the validation done only at debugging time in a
high-volume system since validation is expensive (See (d) in Fig. 1.2). Using XML Spy editor to
generate XSD from XML.

Yuvaraj G 3 yuvashan21@gmail.com
API:
JAX-WS,SAAJ,JAX-RS are APIs implemented for SOAP and REST respectively.

JAX-WS: JAX-WS is the successor to JAX-RPC. It requires Java 5.0, and is not backwards-
compatible to JAX-RPC .Uses RPC(Remote Procedure Call), create Stub and interact directly with the
skeleton methods and supports asynchronous communication. .The Configuration is managed by
annotations therefore Java 5 or higher is required.

JAX-RS : The Java API for RESTful Web Services. Jersey, the reference implementation of JAX-RS,
implements support for the annotations.

SAAJ: Uses SOAP Messages: Interact in terms of these msgs. SAAJ API is used to create/send/receive
SOAP msgs. It operates on a lower level than JAX-RPC or JAX-WS, both of which will use SOAP
envelopes based on SAAJ if needed. Another specification/API for using SOAP envelopes with or
without attachments.

Implementation of Web Services has two Approaches:


1)Bottom to Top(Contract Last)Approach: Generating WSDL files from Implementation Classes (Learners
Level).
2)Top to Bottom(Contract First) Approach: Real time, where interfaces and service classes will be created
from WSDL (Better Approach).you start with the WSDL contract, and use Java to implement said contract.

(WADL in REST) , Apache CXF tool is using to create corresponding java files for
WSDL files. WSImport in java\bin also used to create the corresponding java files for WSDL files
without using any editors like Eclipse ,Netbeans , etc.

How to get WSDL ?

REST APP : http://ip:portno/contextpath/application.wadl


SOAP APP : http://ip:portno/contextpath/url-pattern?wsdl

Note: Another need to get WSDL is to ensure that App -Web services deployed successfully in server.

How to create WSDL to java(Using your WSImport - To create Stub, which is needed
for App1 to interact with App2):

Open your command line, and enter the wsimport command(ensure that to set the java path):

i).CD %CLIENT_PROJECT_HOME%\src
ii).wsimport –s . http://ip:portno/contextpath/url-pattern?wsdl

How to Create JAXB.bat(See (a) in Fig. 1.2) :


i)set JAXB_HOME =””
ii)set JAXB_HOME=”d:\yourdirectoryname(generate_jaxb_java_classes)\jaxb-ri_20009078”
iii)%JAXB_HOME%\bin\xjc.bat -xlocator -b d:\generate_jaxb_java_classes\binding.xjb -d
d:\generate_jaxb_java_classes\your_xsd_file_name.xsd iv)pause

Yuvaraj G 4 yuvashan21@gmail.com
How to create WSDL to JAVA.bat(Using Apache CXF tool):

i)set WSDL_DIR =”d:\your_wsdl_dir”


ii)set PROJ_DIR =”d:your_proj_dir”
iii)call d:\apache-cxf-2.1.9\bin\wsdl2java -d %PROJ_DIR%\src -compile -classdir %PROJ_DIR%\src
-all -verbose %WSDL_DIR%\your_wsdl_file_name.wsdl
iv)pause

How to do Unmarshal(See (b) in Fig. 1.2):

i)JAXBContext jc =null;
ii)Unmarshaller u = null;
iii)Reader reader =null;
iv)ClassName(PurchaseOrderXml) obj = null;
v)String xml_file_content =
“<PurchaseOrderXml><ordId>123<ordId><ordQty>20<ordQty><PurchaseOrderXml>”;
|----------------> Corresponding class name for the xml root element

vi) jc = JAXBContext.newInstance(“Package name with class”);


vii) u = jc.createUnmarshaller();
viii) obj = (ClassName)u.unmarshall(new File(“your xml_file_name with actualpath”));
(or)
viii) reader = new StringReader(xml_file_content);
ix) InputSource insrc = new InputSource(reader);
x) obj = (ClassName)u.unmarshall(insrc);

How to do Marshal(See (c) in Fig. 1.2):

JAXBContext jc = null;
Marshaller m = null;
String xml_file_content = null;
StringWriter sw = new StringWritter();
jc = JAXBContext.newInstance(“package name”);
m = jc.createMarshaller();
synchronized(m)
{
m.marshall(className,sw)
|-------------> Corresponding class name for the xml root element
}
xml_file_content =sw.toString();//write the string(xml_file_content) to one xml file.

Required jar files for JAX-WS,JAX-RS,SAAJ,AXIS:


1)asm-3.3.1.jar,2)jaxb-api-2.2.6.jar,3)jaxb-impl-2.2.5.jar,4)jaxb-xjc-2.2.5.jar,5)jaxws-api-2.2.1.jar,6)jaxws-rt-
2.1.7.jar,7)log4j.jar,8)neethi-3.0.2.jar,9)policy.jar.10)saaj-api-1.3.4.jar,11)saaj-impl-1.3.18.jar,12)stax-
ex.jar,13)streambuffer-0.8.jar,14)jersey-client-1.12.jar,15)jersey-core-1.7.jar,16)jersey-server-1.7.jar,17)js-
1.7R2.jar,18)jsr311-api-1.1.1.jar,19)jaxrpc.jar,20)axis.jar,21)commons-discovery-0.2.jar,22)commons-
logging.jar,23)saaj.jar,24)wsdl4j.jar

Yuvaraj G 5 yuvashan21@gmail.com
WSDL Elements mapping to Java

WSDL and XML construct Java Construct


1)wsdl:targetNamespace(eg:http://example.com) package(name: com.example)
2)a)wsdl:service(where is the service located?-The a)Service Locator Class(Most commonly, this
service element defines the address for invoking the includes a URL for invoking the SOAP service).
specified service).
b)wsdl:port which contains in the wsdl:service b)port accessor method in Service Locator Class.
wsdl:binding(The binding element provides STUB. Bindings can be made available via
specific details on how a portType operation will multiple transports, including HTTP GET, HTTP
actually be transmitted over the wire). POST, or SOAP.
Style(attribute)=rpc(or)document.
Use (attribute) =encoded (or) literal
wsdl:port Type(what operations will be Interface(SEI)
supported?)
wsdl:operation SEI Method
wsdl:message The SEI method signature typically is
determined by the wsdl:message
wsdl:message-input Parameters
wsdl:message-output Return
wsdl:message-fault Throws
xsd:complexType User defined exceptions,Java beans,Arrays

xsd:simpleType Java bean properties ,Primitive types

Document versus RPC, literal versus encoded


The WSDL file defines the format of the SOAP message(how to send request) that are transmitted
1. wsdl:operation – <ns:display> //display is the method Name
2. wsdl:message-input(parameter) name -- <a>
RPC/ENCODED RPC/Literal DOCUMENT/LITERAL DOCUMENT/LITERAL
wrapped
i)<soap:body> <soap:body> <soap:body> <soap:body>
ii)<ns:display> <ns:display> --nothing will come here-- <ns:display_wrapper>
iii) <a <a>ABC</a> <a>ABC</a> <a>ABC</a>
xsi:type="xsd:string">
ABC</a>
iv)</ns:display> </ns:display> ---nothing---- <ns:display_wrapper>
v)</soap:body> </soap:body> </soap:body> </soap:body>

Yuvaraj G 6 yuvashan21@gmail.com
Best Web Service Testing tools:
I would say SoapUI, it is perfectly for installation and implementation. Now if you know some
about Web Service, you can start practice with SoupUI and xmethods.net. To test App2 without App1
use SoapUI.
The four types of JAXB binding declarations(XML-to-Java datatype binding and the customization
namespace prefix):
i)Global Binding Declarations
ii)Class Binding Declarations
iii)Schema Binding Declarations
iv)Property Binding Declarations

Note:
(1)-Consumer-consumes the data
(2)-Producer -Produce the data.
(3)-App1 can be Stand Alone App or Web Application

Yuvaraj G 7 yuvashan21@gmail.com

You might also like