{"id":394,"date":"2009-02-11T10:54:11","date_gmt":"2009-02-11T15:54:11","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=394"},"modified":"2009-09-19T22:23:34","modified_gmt":"2009-09-20T03:23:34","slug":"jaxws-example","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=394","title":{"rendered":"JAX-WS Example"},"content":{"rendered":"<p>Steps to write a jaxws service (both service and client)<\/p>\n<p>*Create an Eclipse java project.<br \/>\n*Add all jar files from jaxws lib library to project classpath.<br \/>\n*Create a directory named &#8220;lib&#8221;.<br \/>\n*Copy log4j.jar to lib.<br \/>\n*Add lib\/log4j.jar to project classpath.<br \/>\n*Create a directory named &#8220;etc&#8221;.<br \/>\n*Create a wsdl file in &#8220;etc&#8221; directory (e.g. testswa.wsdl).<\/p>\n<pre lang=\"xml\">\r\n<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<wsdl:definitions name=\"TestSwa\"\r\n  targetNamespace=\"http:\/\/test.com\/testSwa\" \r\n  xmlns:types=\"http:\/\/test.com\/testSwa\/types\" \r\n  xmlns:xsd=\"http:\/\/www.w3.org\/2001\/XMLSchema\" \r\n  xmlns:soap=\"http:\/\/schemas.xmlsoap.org\/wsdl\/soap\/\" \r\n  xmlns:wsdl=\"http:\/\/schemas.xmlsoap.org\/wsdl\/\" \r\n  xmlns:tns=\"http:\/\/test.com\/testSwa\" \r\n  xmlns:mime=\"http:\/\/schemas.xmlsoap.org\/wsdl\/mime\/\">\r\n\r\n<wsdl:types>\r\n      <xsd:schema \r\n        targetNamespace=\"http:\/\/test.com\/testSwa\/types\"\r\n        elementFormDefault=\"qualified\" \r\n        xmlns:xsd=\"http:\/\/www.w3.org\/2001\/XMLSchema\" \r\n        xmlns:xmime=\"http:\/\/www.w3.org\/2005\/05\/xmlmime\" \r\n        xmlns:wsi=\"http:\/\/ws-i.org\/profiles\/basic\/1.1\/xsd\" \r\n        xmlns:swatype=\"http:\/\/test.com\/testSwa\/types\">\r\n        <xsd:import namespace=\"http:\/\/ws-i.org\/profiles\/basic\/1.1\/xsd\" \r\n          schemaLocation=\"http:\/\/ws-i.org\/profiles\/basic\/1.1\/swaref.xsd\"><\/xsd:import>\r\n\r\n        <xsd:complexType name=\"SimpleSwaType\">\r\n          <xsd:sequence>\r\n            <xsd:element name=\"Attachment\" type=\"wsi:swaRef\"><\/xsd:element>\r\n          <\/xsd:sequence>\r\n        <\/xsd:complexType>\r\n      <\/xsd:schema>\r\n<\/wsdl:types>\r\n\r\n  <wsdl:message name=\"getRequest\">\r\n    <wsdl:part name=\"docId\" type=\"xsd:string\"><\/wsdl:part>\r\n  <\/wsdl:message>\r\n  <wsdl:message name=\"getResponse\">\r\n    <wsdl:part name=\"getReturn\" type=\"types:SimpleSwaType\"><\/wsdl:part>\r\n  <\/wsdl:message>\r\n  <wsdl:portType name=\"TestSwaPortType\">\r\n\r\n      <wsdl:operation name=\"get\">\r\n        <wsdl:input message=\"tns:getRequest\" name=\"getRequest\"><\/wsdl:input>\r\n        <wsdl:output message=\"tns:getResponse\" name=\"getResponse\"><\/wsdl:output>\r\n      <\/wsdl:operation>\r\n  <\/wsdl:portType>\r\n\r\n  <wsdl:binding name=\"TestSwaSoapBinding\"\r\n    type=\"tns:TestSwaPortType\">\r\n\r\n    <soap:binding style=\"rpc\"\r\n      transport=\"http:\/\/schemas.xmlsoap.org\/soap\/http\" \/>\r\n    <wsdl:operation name=\"get\">\r\n      <soap:operation soapAction=\"http:\/\/test.com\/testSwa\/get\" \/>\r\n      <wsdl:input name=\"getRequest\">\r\n        <soap:body use=\"literal\"\r\n          namespace=\"http:\/\/test.com\/testSwa\" \/>\r\n      <\/wsdl:input>\r\n      <wsdl:output name=\"getResponse\">\r\n        <soap:body use=\"literal\"\r\n          namespace=\"http:\/\/test.com\/testSwa\" \/>\r\n      <\/wsdl:output>\r\n    <\/wsdl:operation>\r\n  <\/wsdl:binding>\r\n\r\n  <wsdl:service name=\"TestSwaService\">\r\n    <wsdl:port name=\"TestSwaPort\" binding=\"tns:TestSwaSoapBinding\">\r\n        <soap:address location=\"http:\/\/localhost:8080\/services\/testSwa\"\/>\r\n    <\/wsdl:port>\r\n<\/wsdl:service>\r\n\r\n<\/wsdl:definitions>\r\n<\/pre>\n<p>*Create a web.xml file in &#8220;etc&#8221; directory.<\/p>\n<pre lang=\"xml\">\r\n<web-app version=\"2.4\" xmlns=\"http:\/\/java.sun.com\/xml\/ns\/j2ee\"\r\n       xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n       xsi:schemaLocation=\"http:\/\/java.sun.com\/xml\/ns\/j2ee http:\/\/java.sun.com\/xml\/ns\/j2ee\/web-app_2_4.xsd\">\r\n  <description>Test Swa<\/description>\r\n  <display-name>Test Swa<\/display-name>\r\n  <listener>\r\n      <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener<\/listener-class>\r\n  <\/listener>\r\n  <servlet>\r\n      <description>JAX-WS endpoint - Test Swa<\/description>\r\n      <display-name>Test Swa<\/display-name>\r\n      <servlet-name>testswa<\/servlet-name>\r\n      <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet<\/servlet-class>\r\n      <load-on-startup>1<\/load-on-startup>\r\n  <\/servlet>\r\n  <servlet-mapping>\r\n      <servlet-name>testswa<\/servlet-name>\r\n      <url-pattern>\/testswa<\/url-pattern>\r\n  <\/servlet-mapping>\r\n  <session-config>\r\n      <session-timeout>60<\/session-timeout>\r\n  <\/session-config>\r\n<\/web-app>\r\n<\/pre>\n<p>*Create a sun-jaxws.xml file in &#8220;etc&#8221; directory.<\/p>\n<pre lang=\"xml\">\r\n<endpoints xmlns='http:\/\/java.sun.com\/xml\/ns\/jax-ws\/ri\/runtime' version='2.0'>\r\n  <endpoint\r\n      name=\"testSwa\"\r\n      implementation=\"swa.server.TestSwaImpl\"\r\n      wsdl=\"WEB-INF\/wsdl\/testswa.wsdl\"\r\n      service='{http:\/\/test.com\/testSwa}TestSwaService'\r\n      port='{http:\/\/test.com\/testSwa}TestSwaPort'\r\n      url-pattern=\"\/testswa\"\r\n      enable-mtom=\"false\"\/>\r\n<\/endpoints>\r\n<\/pre>\n<p>*Create an Ant build file (build.xml)<br \/>\n<a href='https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2009\/09\/test-swa_build.xml'>build.xml<\/a><br \/>\n*Run &#8220;build-server-wsdl&#8221; ant task to generated server classes.<br \/>\n*Create a new package (e.g. swa.server) for server implementation class.<br \/>\n*Create a new implementation class (e.g. swa.server.TestSwaImpl) to implement generated PortType class.<br \/>\n*Annotate implementation class<\/p>\n<pre lang=\"java\">@WebService (endpointInterface = \"swa.server.TestSwaPortType\")<\/pre>\n<pre lang=\"java\">\r\npackage swa.server;\r\n\r\nimport java.io.File;\r\n\r\nimport javax.activation.DataHandler;\r\nimport javax.activation.DataSource;\r\nimport javax.activation.FileDataSource;\r\nimport javax.jws.WebService;\r\n\r\nimport org.apache.log4j.*;\r\n\r\n@WebService (endpointInterface = \"swa.server.TestSwaPortType\")\r\npublic class TestSwaImpl implements TestSwaPortType {\r\n  Logger log = LogManager.getLogger(TestSwaImpl.class);\r\n  \r\n  public SimpleSwaType get(String docId) {\r\n    log.debug(\"Getting...\");\r\n    File file = new File(\"c:\/temp\/testswa.txt\");\r\n    DataSource ds = new FileDataSource(file);\r\n    DataHandler dh = new DataHandler(ds);\r\n    SimpleSwaType swatype = new SimpleSwaType();\r\n    swatype.setAttachment(dh);\r\n    return swatype;\r\n  }\r\n\r\n}\r\n<\/pre>\n<p>*Run &#8220;server&#8221; ant task to regenerated, compile and deploy service to Tomcat server.<br \/>\n*Run &#8220;client&#8221; ant task to generated client classes.<br \/>\n*Create a new package (e.g. swa.client) for client test class.<br \/>\n*Create a new client class (e.g. TestSwaClient)<\/p>\n<pre lang=\"java\">\r\npackage swa.client;\r\n\r\nimport java.io.FileWriter;\r\nimport java.io.IOException;\r\nimport java.io.InputStream;\r\nimport java.util.Map;\r\n\r\nimport javax.activation.DataHandler;\r\nimport javax.xml.ws.BindingProvider;\r\n\r\nimport org.apache.log4j.LogManager;\r\nimport org.apache.log4j.Logger;\r\n\r\nimport com.sun.xml.ws.developer.JAXWSProperties;\r\n\r\npublic class TestSwaApp {\r\n\r\n  static Logger log = LogManager.getLogger(TestSwaApp.class);\r\n  \/**\r\n   * @param args\r\n   *\/\r\n  public static void main(String[] args) {\r\n    log.info(\"Start...\");\r\n    TestSwaPortType port = new TestSwaService().getTestSwaPort();\r\n        Map<String, Object> ctxt = ((BindingProvider)port).getRequestContext();\r\n        ctxt.put(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, 8192); \r\n\r\n        SimpleSwaType swatype = port.get(\"1\");\r\n        DataHandler att = swatype.getAttachment();\r\n        log.info(\"Got swa attachment: \" + att.getContentType());\r\n        FileWriter os = null;\r\n        InputStream is = null;\r\n        try{\r\n          is =att.getInputStream();\r\n          os = new FileWriter(\"c:\/temp\/testswa_received.txt\",true);\r\n          int c;\r\n          while ((c = is.read()) != -1){\r\n            os.write(c);\r\n          }\r\n        }catch(Exception e){\r\n          log.error(e);\r\n        }finally{\r\n          if (os != null){\r\n            try {\r\n          os.close();\r\n        } catch (IOException e) {\r\n          log.error(e);\r\n        }\r\n          }\r\n          if (is != null){\r\n            try {\r\n          is.close();\r\n        } catch (IOException e) {\r\n          log.error(e);\r\n        }\r\n          }\r\n        }\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8220;lib&#8221;. *Copy log4j.jar to lib. *Add lib\/log4j.jar to project classpath. &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=394\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[41],"tags":[],"class_list":["post-394","post","type-post","status-publish","format-standard","hentry","category-jax-ws"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-6m","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=394"}],"version-history":[{"count":17,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/394\/revisions"}],"predecessor-version":[{"id":1184,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/394\/revisions\/1184"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}