Escolar Documentos
Profissional Documentos
Cultura Documentos
Web Services
with
XML-RPC – one of the earlier web services formats that was widely used;
still around today
Java standard APIs for web services include JAX-RPC (Java XML-Remote
Procedure Call) and SAAJ (SOAP with Attachments for Java)
uses set of XML entities to describe web
services
Example of a skeleton WSDL descriptor
<?xml version= “1.0” encoding = “UTF -8”?>
<wsdl:definitions
targetNamespace= “myserver.com/services/peopleFinder”
xmlns:wsdl= “schemas.xmlsoap.org/wsdl/”>
<wsdl:types>
------------------------------------------------------------------------------
<wsdl:message name= “message1”>
-----------------------------------------------------------------------------
<wsdl:portType name = “PeopleFinder”>
<wsdl:operation name = “findPeople” parameterOrder= “args”>
------------------------------------------------------------------------------
<wsdl:binding name = “peopleFinderSoapBinding”
------------------------------------------------------------------------------
<wsdl:service name = “PeopleFinderImplService”>
<wsdl:port binding = “impl: peopleFinderSoapBinding”
Name = “peopleFinder”>
------------------------------------------------------------------------------
</wsdl:definitions>
WSDL continued
Entities:
data types – building blocks for SOAP messages; can be simple as one string, or
a complicated XML structure with internal hierarchy
Example:
<?xml version= “1.0” encoding = “UTF -8”?>
<wsdl:definitions . . . >
<wsdl:types>
<schema . . . >
<complexType name = “Person”>
<sequence>
<element name= “firstName” nillable = “true”
Type = “soapenc:string”/>
-----------------------------------------------------------------------------
<sequence>
</complexType>
</schema>
</wsdl:definitions . . . >
WSDL continued
Entities:
messages – one or more data types grouped together; support
inputs, outputs, and faults used by operations
Example:
<wsdl:definitions . . . >
-------------------------------------------------
<wsdl:message name= “findPersonResponse”>
<wsdl:part name= “findPersonReturn” type = “impl:Person”/>
<wsdl:message>
----------------------------------------------------
</wsdl:definitions>
WSDL continued
Entities:
operation – an atomic unit of work for a web service; invoked by clients
Example:
----------------------------------------------------------
<wsdl:portType name = “PeopleFinderImpl”>
<wsdl:operation name = “findPerson” parameterOrder= “args”>
<wsdl:input message = “impl:findPersonRequest”
name = “findPersonRequest”/>
<wsdl:output message = “impl:findPersonResponse”
name = “findPersonResponse”/>
<wsdl:fault message = “impl:BadArgumentFault”
name = “BadArgumentFault”/>
----------------------------------------------------------------
<wsdl:operation>
<wsdl:portType>
WSDL continued
Entities:
port types – grouping of operations into sensible interfaces; represents a
conceptual description of a web service entry point
binding – describes how a particular port type has been bound to a
communication and encoding protocol, such as HTTP and SOAP
Example:
---------------------------------------------------------------------------
<wsdl:service name = “peopleFinderImplService”>
<wsdl:port binding= “impl:PeopleFinderSoapBinding” name = “peopleFinder”>
<wsdlsoap:address
location = http://myserver.com:8080//services/peopleFinder”/>
</wsdl:port>
</wsdl:service>
-----------------------------------------------------------------------------
Web Service Styles and Encoding
web service made up of ports
each port has one or more operations
each operation has input, output, and fault messages
each message is composed of XML data types
SOAP messages consumed and produced by web service
need a way to identify which operation in which port is in effect,
and how message types care expressed in XML of the
message
Styles: The style of a message refers to how the XML structure of the
SOAP body is structured; the two options for the style are RPC and
Document
RPC – The XML of the SOAP body is sturctured to match the
operation and message definitions.
Document – The root element of the SOAP body is matched to the
operation name, but the contents of the root element are not
reflective of the signature of the operation.
Web Service Styles and Encoding
Encoding: The encoding determines how data is encoded
into XML within the message. The two options for the
encoding of service messages are:
Literal – This option specifies that the XML structure of
the data within the message will be totally specified by
XML Schema elements referenced in the WASDL
description of the service.
SOAP-encoded –This option requests that the message
data follow the default encoding rules( and XML
structures) specified in the SOAP specification.
Java Web Services
There are several standards APIs for
implementing and using web services in
Java. The principal ones are the Java API
for XML Remote Procedure Calls (JAX-
RPC), the SOAP with Attachments API for
Java (SAAJ), and the Web services for
J2EE specification. JAX-RPC and SAAJ
are APIs aimed at helping you develop
both web services and web service clients.
JAX-RPC and SAAJ
JAX-RPC and SAAJ together provide a
standard Java framework for consuming
SOAP messages from web services and
for generating SOAP messages and
delivering them to web services. The web
service APIs in Java use a different set of
protocols to accomplish their remote
object ans messaging services.
Mapping SOAP and WSDL to Java
JAX-RPC defines standard mappings from SOAP and WSDL
entities to Java entities and back again.
example of a mapping:
XML type:
xsd:anyURI
Java type:
java.lang.String
In all mappings, the xsd: prefix on an XML type
refers to an entity defined in the XML Schema
basic data types. Each XML data type has two Java
mappings. The first one is used by default for that XML type and the
second one is used if the XML entity is used in a context in which its
presence is optional.
Mapping SOAP and WSDL to Java
continued
When mapping Java entities into XML/SOAP entities, the process is
done in reverse. Java types are mapped to the same XML data
type, but the wrapper class is mapped with the nillable attribute
enabled.For example, a Java int, will be mapped to an XML Schema
like this:
<xsd:element name=“myintvar” type=“xsd:int”/>
while a java.lang.Integer will be mapped like this:
<xsd:element name=“myIntVar” type=“xsd:int” nillable=“true”/>
Finally, in addition to these mappings, JAX-RPC defines a set of
standard mappings from WSDL entities to Java entities. This
mapping is used in both directions by Java web service engine.
WSDL entities describing an external web service are mapped into
Java entities for use by a Java client of the web service.
Web Service Deployment
The web services for J2EE specification sits next
to JAX-RPC and SAAJ as a complementary
standard, aimed at defining standard
approaches for describing web services to Java
application servers and for packaging web
services for deployment to these application
servers. It plays a role for Java-based web
services analogous to the deployment
descriptors and archive formats defined for web
components and EJB components.
Writing Web Service Clients
Client of a service wants to use that service to get
something done
If the WSDL for our Echo service were published at the URL
http://mywebservices.com/services/echo?wdl, we would then use
WSDL2Java to generate client stubs from the command line: >java
org.apache.axis.wsdl.WSDL2Java –o /home/dev/generated-src
http://mywebservices.com/services/echo?wsdl
EchoService interface
Same name of our service in WSDL which this Java
interface is mapped from
For each port defined in the service, there is a
get<portname>() method that returns an instance of
Java class mapped from that port
Ours is named getechoPort() because the bound port
is named echoPort in the service binding and in our
WSDL, (note that accessor methods are named after
the port binding, not the port definitions, because a
single port can be bound to multiple protocols or
physical endpoint URLs in the WSDL file)
Static Proxy Approach - continued
(Writing Web Service Clients – EchoService example)
Web service engine also needs to provide concrete implementations
of these mapped interfaces for the client to use
Before a stateless session EJB can serve as a web service, its EJB
deployment descriptor entry needs to have a <service-endpoint> entry
added t it, to indicate the Java interface that its web service will expose:
<service-endpoint>
com.oreilly.jent.people.ejb.PeopleFinder</service-endpoint>