Java web client using JAX WS

yazooalbumΑσφάλεια

3 Νοε 2013 (πριν από 3 χρόνια και 9 μήνες)

74 εμφανίσεις






Java web client using JAX WS




Table of Contents


Java web client using JAX WS

................................
................................
................................
............................
1

System Requirements
:
................................
................................
................................
...........................

2

Client Code Auto Generation using JAX
-
WS
................................
................................
.......................

2

Implement Client and Header Handlers

................................
................................
................................

3

Service Initialization

................................
................................
................................
.........................

3

Handler Resolution

................................
................................
................................
...........................

3

Object Creation

................................
................................
................................
................................
.

3

Finally, call the web service through SEI

................................
................................
.........................

4

Code Files (For VehiclePriceCalculation service only)

................................
................................
........

4




















System Requirements
:


JDK 1.6: Download from
http://www.oracle.com/technetwork/java/javase/downloads/index.html

Java IDE (optional): for example IDEA, Eclipse, NetBeans


Client Code Auto Genera
tion using JAX
-
WS

Java API for XML
-
Based Web Services (JAX
-
WS) tooling supports generating Java artifacts needed to
develop static JAX
-
WS Web services clients when starting with a Web Services Description Language
(WSDL) file.

The static client programming model for JAX
-
WS is called the dynamic proxy client. The dynamic
proxy client invokes a Web service based on a service endpoint interface that is provided. After a proxy
is created, the client application can invoke methods on

the proxy just like a standard implementation
of those interfaces. For JAX
-
WS Web service clients using the dynamic proxy programming model,
use the JAX
-
WS tool, wsimport, to process a WSDL file and generate portable Java artifacts that are
used to create

a Web service client. Create the following portable Java artifacts using the
wsimport

tool:



Service endpoint interface (SEI)



Service class



Java Architecture for XML Binding (JAXB) generated type values which are Java classes
mapped from XML schema types



One of the most important benefits of using JAX WS is that it is part of the Java Specifications and
does not require any third party jars to be loaded in the classpath


Simple steps to follow


1.

Create directory where auto code needs to be generated, let
’s say D:
\
Projects
\
client
\
java
\
src or
/home/<user>/projects/ client/java/

2.

Create customBinding.xml with following content(attached in the

Code Files

section below)



3.

Open command prompt

4.

Go to directory created above

cd

D:
\
Projects
\
client
\
java
\
src

5.

Run following command to start code generation process:

wsimport
-
s .

b <path to the above created xml file> <wsdl file path>

For e.g. D:
\
Projects
\
client
\
java
\
src>

wsimport
-
s D:
\
Projects
\
custom
\
src
-
b customBinding.xml
h
ttps
://services.motorregister.dk/Integration/VehiclePriceCalculation.svc?wsdl


For more information run
wsimport

help











The above generated code can be compiled and added to a Java archive(jar) file and ported to any
application as a single unit.


Implemen
t Client and Header Handlers




A Client would be the point of interaction for the application trying to integrate with the webservice.


Client class would interact with Service Endpoint Interface created above to make a call to webservice.


Following exp
lanation would present a sample java web client created for VehiclePriceCalculation

Service Initialization

First part of Client would be initialization of service endpoint.


Sample Code Snippet:





Handler Resolution

The JAX
-
WS programming model provides an application handler facility that enables us to
manipulate a message on either an inbound or an outbound flow. This Handler sniffs the outgoing
SOAP Requests and adds the required security credentials. If the securi
ty policy or framework changes,
all we need to do is create a new handler and associate it with the client, so it won't require any code
changes at the client end.


Sample Code Snippet:



OasisHeaderHandler.java

is attached in the next section.

Object Cr
eation







The wsimport has generated type values which are Java classes mapped from XML schema types.
These types are essentially the input and output object types of the service endpoint interface.


Sample Code Snippet:




Finally, call the web service thr
ough SEI



Sample Code Snippet:




Code Files (For VehiclePriceCalculation service only)


Custom Binding XML



customBinding.xml

Generated code:

src.rar

Client Classes:


VehiclePriceCalculationClient.java






Handler Class:




OasisHeaderHandler.java

Complete Package



client.rar