Developing Simple Web Services by Using JWSDP

0 comments

  1. Start new J2EE project WebServices.
  1. Create directories WEB-INF/src (Source), WEB-INF/classes ( .class files), WEB-INF/ lib (jars) and WEB-INF/wsdl.
  1. Create new interface com.j2ee.ws.Hello as
package com.ws.j2ee;
import java.rmi.Remote;
import java.rmi.RemoteException;
/**
 * @author Peter Arockiaraj
 */
public interface Hello extends Remote {
      public String sayHello(String name)throws RemoteException;
}
  1. Create a class HelloWS that implements the above interface
package com.ws.j2ee;
import java.rmi.RemoteException;
/**
 * @author Peter Arockiaraj
 */
public class HelloWS implements Hello{
/* (non-Javadoc)
* @see com.ws.j2ee.Hello#sayHello(java.lang.String)
*/
public String sayHello(String name) throws RemoteException {
      // TODO Auto-generated method stub
      return "Hello " + name;
}
}
  1. Create config.xml file inside WEB-INF with the following contents
<?xml version="1.0" encoding="UTF-8"?> <configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config"> <service name="HelloService" targetNamespace="http://com.ws.j2ee/jws/hello" typeNamespace="http://com.ws.j2ee/jws/hello/types" packageName="com.ws.j2ee"> <interface name="com.ws.j2ee.Hello"/> </service> </configuration>


  1. Generate WSDL and Tie classes using wscompile using the following command
wscompile -classpath ./WEB-INF/classes -gen:server -f:rpcliteral -mapping META-INF/jaxrpc-mapping.xml -keep META-INF/config.xml
This generates a WSDL file for  RPC Literal messaging and a jaxrpc-mapping file and server side tie files.
  1. Copy  jaxrpc-mapping.xml into WEB-INF
  1. Copy HelloService.wsdl into WEB-INF/wsdl
  1. Copy the following files  from
jwsdp/jax-rpc/lib/jaxrpc-impl.jar into WEB-INF/lib
jwsdp/jax-rpc/lib/jaxrpc-spi.jar into WEB-INF/lib
jboss/server/default/lib/activation.jar into WEB-INF/lib
jwsdp/jaxp/dom.jar into WEB-INF/lib
jwsdp/jaxp/sax.jar into WEB-INF/lib
jwsdp/jaxp/xalan.jar into WEB-INF/lib
jwsdp/jaxp/xercesImpl.jar into WEB-INF/lib
  1. Add the .jar files into build path.
  2. Create web.xml in WEB-INF directory.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <servlet> <servlet-name>HelloService</servlet-name> <servlet-class>com.ws.j2ee.HelloWS</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloService</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>



  1. Create webservices.xml file inside WEB-INF as


    <?xml version="1.0" encoding="UTF-8"?> <webservices xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://www.ibm.com/webservices/xsd/j2ee_web_services_1_1.xsd" version="1.1"> <webservice-description> <webservice-description-name>HelloService</webservice-description-name> <wsdl-file>WEB-INF/wsdl/HelloService.wsdl</wsdl-file> <jaxrpc-mapping-file>WEB-INF/jaxrpc-mapping.xml</jaxrpc-mapping-file> <port-component> <port-component-name>Hello</port-component-name> <wsdl-port>HelloPort</wsdl-port> <service-endpoint-interface>com.ws.j2ee.Hello</service-endpoint-interface> <service-impl-bean> <servlet-link>HelloService</servlet-link> </service-impl-bean> </port-component> </webservice-description> </webservices>

  1. Create a package with following structure ( call it hellows-rpc.war)
WEB-INF/classes/*.class files
WEB-INF/lib/*.jar files
WEB-INF/wsdl/HelloService.wsdl file
WEB-INF/web.xml,. jaxrpc-mapping.xml , config.xml file
  1. Run Build and Create hellows-rpc.war.
  2. Copy file into deploy dir of jBOSS.
  1. After the web service is deployed, the wsdl file is produces at default/data/wsdl and is published at

Writing a WS Client
  1. Create a j2EE project HelloWSClient
  2. Create Folder structure src, classes, lib ,wsdl.
  1.  Create file config.xml inside the project root as
<?xml version="1.0" encoding= "UTF-8"?> <configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config"> <wsdl location= "wsdl\HelloService.wsdl" packageName= "com.ws.clients.gen" /> </configuration>
  1. Copy the .wsdl from http://localhost:8080/hellows-rpc/hello?wsdl into HelloWSClient/wsdl
  1. Generate client side stubs by executing...
wscompile -verbose -gen -d classes -s src -keep config.xml
  1. Copy the following files  from
jwsdp/jax-rpc/lib/jaxrpc-impl.jar into lib
jwsdp/jax-rpc/lib/jaxrpc-spi.jar into lib
jboss/server/default/lib/activation.jar into lib
jboss/server/default/lib/mail.jar into lib
jwsdp/jaxp/dom.jar into lib
jwsdp/jaxp/sax.jar into lib
jwsdp/jaxp/xalan.jar into lib
jwsdp/jaxp/xercesImpl.jar into lib
jwsdp/saaj/saaj-impl.jar into lib
jwsdp/fastinfoset/lib/FastInfoset.jar into lib
jwsdp/saaj/lib/saajImpl.jar into lib
jwsdp/sjsxp/lib/jsr173_api.jar into lib
  1. Add them to build path
  1. Create class  HelloClient.java as
package com.ws.clients;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.ServiceFactory;
import com.ws.clients.gen.Hello;
import com.ws.clients.gen.HelloService_Impl;
/**
 * @author Peter Arockiaraj
 */
public class HelloClient {
 public static void main(String[] args) throws ServiceException,RemoteException,MalformedURLException{
      System.out.println("Hello Web Service Client");
      try{
      System.setProperty("javax.xml.soap.MessageFactory", "com.sun.xml.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl");
       //System.setProperty("javax.xml.soap.SOAPFactory","com.sun.xml.messaging.saaj.soap.ver1_1.SOAPFactory1_1Impl");
       //System.setProperty("javax.xml.rpc.ServiceFactory","com.sun.xml.rpc.client.ServiceFactoryImpl");
       //System.setProperty("javax.xml.soap.SOAPConnectionFactory","javax.xml.soap.SOAPConnectionFactory");
       //System.setProperty("javax.xml.parsers.DocumentBuilderFactory","com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
      HelloService_Impl impl =
            new HelloService_Impl();
      Hello ws = impl.getHelloPort();
    String result = ws.sayHello("test");
      System.out.println("WS output is " + result);
    System.out.println("Dynamic WS call");
      URL url = new URL ("http://localhost:8080/hellows-rpc/hello?wsdl");
      QName qname = new QName("http://com.ws.j2ee/jws/hello","HelloService");
      ServiceFactory factory = ServiceFactory.newInstance();
      Service service = factory.createService(url,qname);
      Hello hello = (Hello) service.getPort(Hello.class);
       System.out.println("hello.hello(" + "test" + ")");
      System.out.println("output:" + hello.sayHello("test"));
      }catch(Exception se){
            System.out.println("ServiceException " + se.getMessage());
      }
}
}
  1. Execute HelloClient.clas
Output is as
Hello Web Service Client
WS output is Hello test
Dynamic WS call
ServiceException null
SOAP request:

0 comments: