Jax WS
Jax WS
In JAX-WS, a web service operation invocation is represented by an XML-based protocol such as SOAP. The SOAP
specification defines the envelope structure, encoding rules, and conventions for representing web service invocations and
responses. These calls and responses are transmitted as SOAP messages (XML files) over HTTP.
Although SOAP messages are complex, the JAX-WS API hides this complexity from the application developer. On the
server side, the developer specifies the web service operations by defining methods in an interface written in the Java
programming language. The developer also codes one or more classes that implement those methods. Client programs are
also easy to code. A client creates a proxy (a local object representing the service) and then simply invokes methods on the
proxy. With JAX-WS, the developer does not generate or parse SOAP messages. It is the JAX-WS runtime system that
converts the API calls and responses to and from SOAP messages.
With JAX-WS, clients and web services have a big advantage: the platform independence of the Java programming
language. In addition, JAX-WS is not restrictive: a JAX-WS client can access a web service that is not running on the Java
platform, and vice versa. This flexibility is possible because JAX-WS uses technologies defined by the World Wide Web
Consortium (W3C): HTTP, SOAP, and the Web Service Description Language (WSDL). WSDL specifies an XML format
for describing a service as a set of endpoints operating on messages.
Several files in the JAX-WS examples depend on the port that you specified when you installed the Enterprise Server. The
tutorial examples assume that the server runs on the default port, 8080. If you have changed the port, you must update the
port number in the following file before building and running the JAX-WS examples:
tut-install/examples/jaxws/simpleclient/src/java/simpleclient/HelloClient.java
This section shows how to build and deploy a simple web service and client. The source code for the service is in tut-
install/examples/jaxws/helloservice/ and the client is in tut-install/examples/jaxws/simpleclient/.
Figure 12–1 illustrates how JAX-WS technology manages communication between a web service and 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. For reference documentation on wsgen, see the Sun GlassFish Enterprise Server v3
Reference Manual.
Together, the wsgen tool and the Enterprise Server provide the Enterprise Server’s implementation of JAX-WS.
These are the basic steps for creating the web service and client:
The @PostConstruct method is called by the container before the implementing class begins responding to web
service clients.
The @PreDestroy method is called by the container before the endpoint is removed from operation.
In this example, the implementation class, Hello, is annotated as a web service endpoint using
the @WebService annotation. Hello declares a single method named sayHello, annotated with
the @WebMethod annotation. @WebMethod exposes the annotated method to web service clients. sayHello returns a
greeting to the client, using the name passed to sayHello to compose the greeting. The implementation class also must
define a default, public, no-argument constructor.
package helloservice.endpoint;
import javax.jws.WebService;
@WebService
public class Hello {
private String message = new String("Hello, ");
@WebMethod
public String sayHello(String name) {
return message + name + ".";
}
}
You can build, package, and deploy the helloservice application using either NetBeans IDE or ant.
Follow these instructions to build, package, and deploy the helloservice example to your Application Server instance using
the NetBeans IDE IDE.
This builds and packages to application into helloservice.war, located in tut-install/examples/jaxws/helloservice/dist/, and
deploys this WAR file to your Application Server instance.
To build and package helloservice using Ant, in a terminal window, go to the tut-
install/examples/jaxws/helloservice/ directory and type the following:
ant
This command calls the default target, which builds and packages the application into an WAR file, helloservice.war,
located in the dist directory.
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.
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
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. Open the Admin Console by typing the following URL in a web browser:
http://localhost:4848/
2. Enter the admin user name and password to log in to the Admin Console.
3. Click Web Services in the left pane of the Admin Console.
4. Click Hello.
5. Click Test.
6. Under Methods, enter a name as the parameter to the sayHello method.
7. Click the sayHello button.
This will take you to the sayHello Method invocation page.
8. Under Method returned, you’ll see the response from the endpoint.
HelloClient is a standalone Java program that accesses the sayHello method of HelloService. It makes this call through a
port, a local object that acts as a proxy for the remote service. The port is created at development time by the wsimport tool,
which generates JAX-WS portable artifacts based on a WSDL file.
When invoking the remote methods on the port, the client performs these steps:
1. Uses the generated helloservice.endpoint.HelloService class which represents the service at the URI of the deployed
service’s WSDL file.
2. Retrieves a proxy to the service, also known as a port, by invoking getHelloPort on the service.
Here is the full source of HelloClient, which is located in the tut-install/examples/jaxws/simpleclient/src/java/ directory.
package simpleclient;
import javax.xml.ws.WebServiceRef;
import helloservice.endpoint.HelloService;
import helloservice.endpoint.Hello;
String name;
if (args.length > 0) {
name = args[0];
} else {
name = "No Name";
}
You will see the output of the application client in the Output pane.
ant
This command calls the default target, which builds and packages the application into a JAR file, simpleclient.jar, located in
the dist directory.
The run the client, type the following command:
ant run