Java Web Services - CURRENT TOPICS IN PROGRAMMING LANGUAGES

concepcionsockSoftware and s/w Development

Aug 15, 2012 (5 years and 27 days ago)

334 views

1

Lecture 22

George
Koutsogiannakis

Summer 2011

CS441


CURRENT TOPICS IN
PROGRAMMING LANGUAGES

Topics


Web Services
-

APIs used in EE5


JAX
-
RPC (before EE5)


JAX
-
WS


JAXB



2

Web Services
-
JAX
-
RPC


JAX
-
RPC enables a
Web Service
endpoint to be developed using
either a Java
Servlet

or Enterprise JavaBeans (EJB) component
model.


A Web service endpoint is deployed on either the Web container or
EJB container based on the corresponding component model.


These endpoints are described using a WSDL document. This WSDL
document can be published in public or private registry, though this
is not required.


A client uses this WSDL document and invokes the Web service
endpoint.


A JAX
-
RPC client can use stubs
-
based, dynamic proxy or dynamic
invocation interface (DII) programming models to invoke a
heterogeneous Web service endpoint.

3

Web Services
-
JAX
-
RPC


RPC stands for Remote Procedure Call.


A protocol used by Microsoft in their systems.


RMI (and its offsprings) is similar to RPC.


JAX
-
RPC allows the communication of a web
service client with a web component/ejb
session bean so that the web service can be
invoked via the business rules model.

4

Web Services
-
JAX
-
RPC

5

Web Services
-
JAX
-
RPC


It is assumed that the client is aware of the Web service and the remote
procedure that it can execute on the Web service. This is what happens:


The client calls the method on the stub that represents the remote
procedure.


The stub executes the necessary routines on the JAX
-
RPC runtime
system.


The runtime system converts this method call into a SOAP message
and transmits the message to the server as an HTTP request.


The server, upon receipt of the SOAP message, invokes the methods
on the JAX
-
RPC runtime. The JAX
-
RPC runtime converts the SOAP
request into a method call.


The JAX
-
RPC runtime then calls the method on the tie object.


Finally, the tie object calls the method on the implementation of the
Web service.


The response to the RPC call is sent in a SOAP response message as an
HTTP response.


6

Web Services
-
JAX
-
RPC


The Web service is represented by two files:
the service definition interface
, and the
corresponding
implementation class.


The service definition interface and the
implementation class are collectively known
as the
service endpoint
.

7

EE5 and Web Services
-
JAX
-
WS


Java EE5 provides annotations for Web
Services.


JAX
-
WS 2.0 is the new API for web services in
the Java EE 5 platform and it is a successor to
JAX
-
RPC.

8

EE5 and Web Services
-
JAX
-
WS


Example comparing the approach between JAX
-
RPC and JAX
-
WS 2.0


JAX
-
RPC Web Service:

public interface
HelloService

extends Remote {

public String
sayHello
(String name) throws
RemoteException
;

}

public class
HelloServiceBean

implements
SessionBean

{

public String
sayHello
(String name)

{


return "Hello "+ name + " from
HelloServiceBean
";

}

}

9

EE5 and Web Services
-
JAX
-
WS


Example using JAX
-
WS 2.0


@
WebService

public class
HelloServiceBean


{


public String
sayHello
(String name)


{



return "Hello "+ name + " from

HelloServiceBean
";


}

}


Notice that:


No need for an interface!


The @
WebService

annotation defines the class as a web service end
point.



10

EE5 and Web Services
-
JAX
-
WS


Here is another example with more annotations:

@WebService(name=”CreditRatingService”,


targetNamespace=”http://example.org”)


public class CreditRating {


@WebMethod(operationName=”getCreditScore”)


public Score getCredit(


@WebParam(name=”customer”)


Customer c


{


// ... implementation code ...


}

}


11

EE5 and Web Services
-
JAX
-
WS

12

Our diagram is now revised to show that the JAX
-
WS runtime environment


is used instead of JAX
-
RPC.

Notice that the service can also be an ejb.

EE5 and Web Services
-
JAX
-
WS


You may specify an explicit interface by adding
the endpointInterface element to the
@WebService annotation in the
implementation class.


You must then provide an interface that
defines the public methods made available in
the endpoint implementation class.

13

EE5 and Web Services
-
JAX
-
WS


The
wsgen tool
and the Application Server
provide the Application Server’s
implementation of JAX
-
WS.


wsgen generates all articrafts (files) needed for
the implementation of the web service.

14

EE5 and Web Services
-
JAX
-
WS


Building a Web Service With Java WSDP 2.0



Write an endpoint implementation class.


Compile the endpoint implementation class.


Generate portable artifacts required for web service by using wsgen
tool.


Package the web service as a WAR file and deploy it.


Code the client class.


Use wsimport tool to generate and compile the web service artifacts needed
to connect to the service.


Compile the client class.


Run the client.



15

EE5 and Web Services
-
JAX
-
WS


Suppose our end point service class is:

package helloservice.endpoint;

import javax.jws.WebService;

@WebService

public class Hello

{


private String message = new String("Hello, ");


public void Hello() {} //constructor


@WebMethod public String sayHello(String name)


{



return message + name + ".";


}


}

16

EE5 and Web Services
-
JAX
-
WS


You can build, package, and deploy the helloservice
application using either NetBeans IDE or ant.


Notice that the WSDL file is generated automaticaly
(as well as the descriptor files needed).


Also, notice that XML is totally hidden from the
developer.


Keep in mind that annotations come with attributes:


i.e. the @WebService annotation has attributes:

17

EE5 and Web Services
-
JAX
-
WS


endpointInterface


name


portName


serviceName


targetNamespace


wsdlLocation


18

EE5 and Web Services
-
JAX
-
WS


Coding the Client


When invoking the remote methods on the port, the client
performs these steps:


Uses the
javax.xml.ws.WebServiceRef

annotation to declare a
reference to a web service.


@
WebServiceRef

uses the
wsdlLocation

element to specify the URI of
the deployed service’s WSDL file.


@
WebServiceRef
(
wsdlLocation
="http://localhost:8080/helloservice/h
ello?wsdl") static
HelloService

service;


Retrieves a proxy to the service, also known as a port, by invoking
getHelloPort

on the service.


Hello
port
=
service.getHelloPort
();
The port implements the SEI
defined by the service.


Invokes the port’s
sayHello

method, passing to the service a name.


String response =
port.sayHello
(
name);


19

EE5 and Web Services
-
JAX
-
WS


public class
HelloClient

{
@
WebServiceRef
(
wsdlLocation
="http://localhost:8080/
helloservice
/
hello?wsdl
")




static
HelloService

service;

………………………………………………………






Hello port =
service.getHelloPort
();


String response =
port.sayHello
(name);


System.out.println
(response);


…………………………………………………………….

20

EE5 and Web Services
-
JAXB


JAX
-
WS delegates the mapping of the Java
language data types to JAXB API.


JAXB stands for Java Architecture for XML Binding.


JAXB converts XML schemas to Java Content trees
(see example in previous lecture of a content tree)
and vice versa.

21

EE5 and Web Services
-
JAXB
Architecture.

22

EE5 and Web Services
-
JAXB
Architecture.


Basically JAXB is the translator of XML
schemas (and data types) to Java and vice
versa.


Since the WSDL describes the XML schema there
is a need to translate that schema to Java.


It is the job of JAXB to do just that.

23

EE5 and Web Services
-
JAXB
Architecture.


XML Schema Type Java Data Type


xsd:string




java.lang.String


xsd:integer




java.math.BigInteger


xsd:int




int


xsd.long




long


xsd:short




short


xsd:decimal




java.math.BigDecimal


xsd:float




float


24

EE5 and Web Services
-
JAXB
Architecture

25

EE5 and Web Services
-
JAXB
Architecture


Generate classes
: An XML schema is used as input to the JAXB binding
compiler to generate JAXB classes based on that schema.


Compile classes
: All of the generated classes, source files, and application
code must be compiled.


Unmarshal
: XML documents written according to the constraints in the
source schema are unmarshalled by the JAXB binding framework. Note
that JAXB also supports unmarshalling XML data from sources other than
files/documents, such as DOM nodes, string buffers, SAX Sources, and so
forth.


Generate content tree
: The unmarshalling process generates a content
tree of data objects instantiated from the generated JAXB classes; this
content tree represents the structure and content of the source XML
documents.


26

EE5 and Web Services
-
JAXB
Architecture


Validate (optional)
: The unmarshalling process optionally involves
validation of the source XML documents before generating the content
tree. Note that if you modify the content tree in step below, you can also
use the JAXB Validate operation to validate the changes before marshalling
the content back to an XML document.


Process content
: The client application can modify the XML data
represented by the Java content tree by means of interfaces generated by
the binding compiler.


Marshal
: The processed content tree is marshalled out to one or more
XML output documents. The content may be validated before marshalling.


27

EE5 and Web Services
-
NetBeans
support.


NetBeans 6.5 provides support for web
services using JAX
-
WS and JAXB.


For more information on how to use NetBeans
to develop a web service by utilizing JAX
-
WS
and JAXB see the NetBeans tutorial at:


http://www.netbeans.org/kb/docs/websvc/jax
-
ws.html

28

Study Guide


EE5 Tutorial text chapter 15.


EE5 Tutorial text chapter 16


NetBeans tutorial online.


EE5 Tutorial chapters 11,12

29