JAX Ws
JAX Ws
Java API for XML Web Services (JAX-WS) is one of a set of Java technologies
used to develop Web services. JAX-WS belongs to what Sun Microsystems
calls the "core Web services" group. Like most of the core group, JAX-WS is
typically used in conjunction with other technologies. Those other
technologies may also come from the core Web services group (JAXB, for
example), as well as from enhanced Web services (WSIT), secure Web
services (WSIT, WS-Security), legacy Web services (JAX-RPC), and systems
management services (WS-Management) groups.
JAX-WS is a fundamental technology for developing SOAP (Simple Object
Access Protocol) and RESTful (Web services that use representational
state transfer, or REST, tools) Java Web services, where JAX-WS is
designed to take the place of the JAVA-RPC (Remote Procedure Call)
interface in Web services and Web-based applications. JAX-WS is also used
to build Web services and corresponding clients that communicate
using XML to send messages or use remote procedure calls to exchange
data between client and service provider.
JAX-WS represents remote procedure calls or messages using XML-based
protocols such as SOAP, but hides SOAP's innate complexity behind a Javabased API. Developers use this API to define methods, then code one or
more classes to implement those methods and leave the communication
details to the underlying JAX-WS API. Clients create a local proxy to
represent a service, then invoke methods on the proxy. The JAX-WS
runtime system converts API calls and matching replies to and from SOAP
messages.
creating a Simple Web Service and Client with JAX-WS
This section shows how to build and deploy a simple web service and
client. The source code for the service is in tutinstall/javaeetutorial5/examples/jaxws/helloservice/ and the client is
in tut-install/javaeetutorial5/examples/jaxws/simpleclient/.
Figure 16-1 illustrates how JAX-WS technology manages communication
between a web service and client.
Figure 16-1 Communication between a JAX-WS Web Service and a
Client
The starting point for developing a JAX-WS web service is a Java class
annotated with the javax.jws.WebService annotation.
The @WebService annotation defines the class as a web service endpoint.
A service endpoint interface or service endpoint
implementation (SEI) is a Java interface or class, respectively, that
declares the methods that a client can invoke on the service. An interface
is not required when building a JAX-WS endpoint. The web service
implementation class implicitly defines an SEI.
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.
You use the endpoint implementation class and the wsgen tool to
generate the web service artifacts that connect a web service client to the
JAX-WS runtime.
Together, the wsgen tool and the Application Server provide the
Application Servers implementation of JAX-WS.
These are the basic steps for creating the web service and client:
1.
2.
3.
4.
5.
6.
Deploy the WAR file. The web service artifacts (which are used to
communicate with clients) are generated by the Application Server
during deployment.
Code the client class.
7.
8.
9.
The implementing class must not be declared final and must not
be abstract.
2.
3.
4.
5.
6.
2.
3.
You can view the WSDL file of the deployed service by requesting the
URL http://localhost:8080/helloservice/hello?WSDL in a web browser. Now
you are ready to create a client that accesses this service.
Undeploying the Service
At this point in the tutorial, do not undeploy the service. When you are
finished with this example, you can undeploy the service by typing this
command:
ant undeploy
The all Task
As a convenience, the all task will build, package, and deploy the
application. To do this, enter the following command:
ant all
Testing the Service without a Client
The Application Server Admin Console allows you to test the methods of a
web service endpoint. To test the sayHello method of HelloService, do the
following:
1.
2.
Enter the admin user name and password to log in to the Admin
Console.
3.
4.
Click Hello.
5.
Click Test.
6.
7.
8.
Under Method returned, youll see the response from the endpoint.
2.
@WebServiceRef(wsdlLocation="http://localhost:8080/helloservice/h
ello?wsdl")
static HelloService service;
3.
4.
Here is the full source of HelloClient, which is located in the tutinstall/javaeetutorial5/examples/jaxws/simpleclient/src/java/ directory.
package simpleclient;
import javax.xml.ws.WebServiceRef;
import helloservice.endpoint.HelloService;
import helloservice.endpoint.Hello;
public class HelloClient {
@WebServiceRef(wsdlLocation="http://localhost:8080/
helloservice/hello?wsdl")
static HelloService service;
public static void main(String[] args) {
try {
HelloClient client = new HelloClient();
client.doTest(args);
} catch(Exception e) {
e.printStackTrace();
}
}
public void doTest(String[] args) {
try {
System.out.println("Retrieving the port from
the following service: " + service);
Hello port = service.getHelloPort();
System.out.println("Invoking the sayHello operation
on the port.");
String name;
if (args.length > 0) {
name = args[0];
} else {
name = "No Name";
}
String response = port.sayHello(name);
System.out.println(response);
} catch(Exception e) {
e.printStackTrace();
}
}
}
Building and Running the Client
You can build and run the simpleclient application using either NetBeans
IDE or ant. To build the client, you must first have deployed helloservice,
as described in Building, Packaging, and Deploying the Service.
Building and Running the Client in NetBeans IDE
Do the following to build and run simpleclient:
1.
2.
3.
4.
5.
6.
You will see the output of the application client in the Output pane.
Building and Running the Client Using Ant
In a terminal navigate to tut-install/examples/jaxws/simpleclient/ and type
the following command:
ant