WebLogic Web Services: Getting Started

     Previous  Next    Open TOC in new window    View as PDF - New Window  Get Adobe Reader - New Window
Content starts here

Implementing a JAX-WS 2.0 Web Service

The following topics describe why and how to implement a JAX-WS 2.0 Web Service:

 


Implementing a JAX-WS Web Service: Overview

Although both Java API for XML-Based RPC (JAX-RPC) 1.1 and Java API for XML Web Services (JAX-WS) 2.0 are supported in this release of WebLogic Server, the WebLogic Web Services documentation concentrates almost exclusively on describing how to create JAX-RPC-based Web Services. This is because all the WS-* specifications (such as WS-Security and WS-ReliableMessaging) and the WebLogic value-added features (such as asynchronous request-response and callbacks) work only with JAX-RPC style Web Services. For this reason, unless you specifically want to create a JAX-WS Web Service, it is assumed that you want to create a JAX-RPC service so that you can take full advantage of all features provided by WebLogic Server. This section, however, describes why and how to implement a JAX-WS 2.0 Web Service.

Reasons to implement a Web Service based on JAX-WS 2.0 include the following:

For additional documentation and examples about programming the preceding features in a JAX-WS Web Service, see the JAX-WS 2.0 User's Guide on java.net.

 


Implementing a JAX-WS Web Service: Guidelines

Implementing a JAX-WS Web Service is similar to implementing a JAX-RPC Web Service, and for the most part you can follow the procedures described in Iterative Development of WebLogic Web Services. There are, however, a few differences as described below:

 


Simple Example of Implementing a JAX-WS Web Service

The following sections show a simple example of implementing a JAX-WS Web Service

Example of a JWS File That Implements a JAX-WS Web Service

The following Java file shows a simple example of implementing a JAX-WS Web Service.

The example uses the standard @javax.jws.WebService annotation to declare that it is a Web Service, and then the common @javax.annotation.Resource annotation to inject the Web Service context into the context variable. With the context one can get information about the Web Service; in this case, the principal user that invokes the service.

package examples.webservices.jaxws;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;
import javax.annotation.Resource;
@WebService()
/**
* This JWS file forms the basis of simple JAX-WS WebLogic Web Service
*
*/
public class JaxWsImpl {
  @Resource
private WebServiceContext context;
  public String sayHello(String message) {
    String principal = context.getUserPrincipal().getName();
    System.out.println("Hello! Here is the passed=in message: " + message + ". And here is the user principal: " + principal + ".");
    return "Here is the message: '" + message + "'. And here is the user principal: '" + principal + "'." ;
  }
}

Specifying a JAX-WS Web Service to the jwsc and clientgen Ant Tasks

The following excerpt from a build.xml Ant build file shows how to use the type attribute of the jwsc Ant task to specify that the task should generate a JAX-WS Web Service, rather than the default JAX-RPC service:

    <jwsc
srcdir="src"
destdir="${ear-dir}">
      <jws file="examples/webservices/jaxws/JaxWsImpl.java"
type="JAXWS"
/>
    </jwsc>

Similarly, the following call to clientgen shows how to specify that the task should generate client-side artifacts used to invoke a JAX-WS Web Service:

<clientgen
type="JAXWS"
wsdl="http://${wls.hostname}:${wls.port}/JaxWsImpl/JaxWsImplService?WSDL"
destDir="${clientclass-dir}"
packageName="examples.webservices.jaxws.client"/>

Example of Invoking a JAX-WS Web Service

The following simple standalone Java client shows how to invoke the Web Service implemented in the preceding sections:

package examples.webservices.jaxws.client;
/**
* This is a simple standalone client application that invokes the
* the <code>sayHello</code> operation of the JaxWs Web service.
*
* @author Copyright (c) 2004 by BEA Systems. All Rights Reserved.
*/
public class Main {
  public static void main(String[] args) {
    JaxWsImplService service = new JaxWsImplService();
JaxWsImpl port = service.getJaxWsImplPort();
    String result = null;
result = port.sayHello("Hi there!");
System.out.println( "Got result: " + result );
}
}

  Back to Top       Previous  Next