Steps to write a jaxws service (both service and client)
*Create an Eclipse java project.
*Add all jar files from jaxws lib library to project classpath.
*Create a directory named “lib”.
*Copy log4j.jar to lib.
*Add lib/log4j.jar to project classpath.
*Create a directory named “etc”.
*Create a wsdl file in “etc” directory (e.g. testswa.wsdl).
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?> <wsdl:definitions name="TestSwa" targetNamespace="http://test.com/testSwa" xmlns:types="http://test.com/testSwa/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://test.com/testSwa" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"> <wsdl:types> <xsd:schema targetNamespace="http://test.com/testSwa/types" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xmime="http://www.w3.org/2005/05/xmlmime" xmlns:wsi="http://ws-i.org/profiles/basic/1.1/xsd" xmlns:swatype="http://test.com/testSwa/types"> <xsd:import namespace="http://ws-i.org/profiles/basic/1.1/xsd" schemaLocation="http://ws-i.org/profiles/basic/1.1/swaref.xsd"></xsd:import> <xsd:complexType name="SimpleSwaType"> <xsd:sequence> <xsd:element name="Attachment" type="wsi:swaRef"></xsd:element> </xsd:sequence> </xsd:complexType> </xsd:schema> </wsdl:types> <wsdl:message name="getRequest"> <wsdl:part name="docId" type="xsd:string"></wsdl:part> </wsdl:message> <wsdl:message name="getResponse"> <wsdl:part name="getReturn" type="types:SimpleSwaType"></wsdl:part> </wsdl:message> <wsdl:portType name="TestSwaPortType"> <wsdl:operation name="get"> <wsdl:input message="tns:getRequest" name="getRequest"></wsdl:input> <wsdl:output message="tns:getResponse" name="getResponse"></wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="TestSwaSoapBinding" type="tns:TestSwaPortType"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="get"> <soap:operation soapAction="http://test.com/testSwa/get" /> <wsdl:input name="getRequest"> <soap:body use="literal" namespace="http://test.com/testSwa" /> </wsdl:input> <wsdl:output name="getResponse"> <soap:body use="literal" namespace="http://test.com/testSwa" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="TestSwaService"> <wsdl:port name="TestSwaPort" binding="tns:TestSwaSoapBinding"> <soap:address location="http://localhost:8080/services/testSwa"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
*Create a web.xml file in “etc” directory.
<web-app version="2.4" 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"> <description>Test Swa</description> <display-name>Test Swa</display-name> <listener> <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class> </listener> <servlet> <description>JAX-WS endpoint - Test Swa</description> <display-name>Test Swa</display-name> <servlet-name>testswa</servlet-name> <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>testswa</servlet-name> <url-pattern>/testswa</url-pattern> </servlet-mapping> <session-config> <session-timeout>60</session-timeout> </session-config> </web-app>
*Create a sun-jaxws.xml file in “etc” directory.
<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'> <endpoint name="testSwa" implementation="swa.server.TestSwaImpl" wsdl="WEB-INF/wsdl/testswa.wsdl" service='{http://test.com/testSwa}TestSwaService' port='{http://test.com/testSwa}TestSwaPort' url-pattern="/testswa" enable-mtom="false"/> </endpoints>
*Create an Ant build file (build.xml)
build.xml
*Run “build-server-wsdl” ant task to generated server classes.
*Create a new package (e.g. swa.server) for server implementation class.
*Create a new implementation class (e.g. swa.server.TestSwaImpl) to implement generated PortType class.
*Annotate implementation class
@WebService (endpointInterface = "swa.server.TestSwaPortType")
package swa.server; import java.io.File; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.jws.WebService; import org.apache.log4j.*; @WebService (endpointInterface = "swa.server.TestSwaPortType") public class TestSwaImpl implements TestSwaPortType { Logger log = LogManager.getLogger(TestSwaImpl.class); public SimpleSwaType get(String docId) { log.debug("Getting..."); File file = new File("c:/temp/testswa.txt"); DataSource ds = new FileDataSource(file); DataHandler dh = new DataHandler(ds); SimpleSwaType swatype = new SimpleSwaType(); swatype.setAttachment(dh); return swatype; } }
*Run “server” ant task to regenerated, compile and deploy service to Tomcat server.
*Run “client” ant task to generated client classes.
*Create a new package (e.g. swa.client) for client test class.
*Create a new client class (e.g. TestSwaClient)
package swa.client; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.util.Map; import javax.activation.DataHandler; import javax.xml.ws.BindingProvider; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import com.sun.xml.ws.developer.JAXWSProperties; public class TestSwaApp { static Logger log = LogManager.getLogger(TestSwaApp.class); /** * @param args */ public static void main(String[] args) { log.info("Start..."); TestSwaPortType port = new TestSwaService().getTestSwaPort(); Map<String, Object> ctxt = ((BindingProvider)port).getRequestContext(); ctxt.put(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, 8192); SimpleSwaType swatype = port.get("1"); DataHandler att = swatype.getAttachment(); log.info("Got swa attachment: " + att.getContentType()); FileWriter os = null; InputStream is = null; try{ is =att.getInputStream(); os = new FileWriter("c:/temp/testswa_received.txt",true); int c; while ((c = is.read()) != -1){ os.write(c); } }catch(Exception e){ log.error(e); }finally{ if (os != null){ try { os.close(); } catch (IOException e) { log.error(e); } } if (is != null){ try { is.close(); } catch (IOException e) { log.error(e); } } } } }