- Start new J2EE project WebServices.
- Create directories WEB-INF/src (Source), WEB-INF/classes ( .class files), WEB-INF/ lib (jars) and WEB-INF/wsdl.
- 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;
}
- 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;
}
}
- Create config.xml file inside WEB-INF with the following contents
- 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.
- Copy jaxrpc-mapping.xml into WEB-INF
- Copy HelloService.wsdl into WEB-INF/wsdl
- 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
- Add the .jar files into build path.
- Create web.xml in WEB-INF directory.
- 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>
- 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
- Run Build and Create hellows-rpc.war.
- Copy file into deploy dir of jBOSS.
- After the web service is deployed, the wsdl file is produces at default/data/wsdl and is published at
Writing a WS Client
- Create a j2EE project HelloWSClient
- Create Folder structure src, classes, lib ,wsdl.
- Create file config.xml inside the project root as
- Copy the .wsdl from http://localhost:8080/hellows-rpc/hello?wsdl into HelloWSClient/wsdl
- Generate client side stubs by executing...
wscompile -verbose -gen -d classes -s src -keep config.xml
- 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
- Add them to build path
- 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());
}
}
}
- Execute HelloClient.clas
Output is as
Hello Web Service Client
WS output is Hello test
Dynamic WS call
ServiceException null
SOAP request:
0 comments:
Post a Comment