{"id":144,"date":"2008-10-16T14:15:26","date_gmt":"2008-10-16T18:15:26","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=144"},"modified":"2011-04-20T15:17:51","modified_gmt":"2011-04-20T20:17:51","slug":"hello-world-with-jax-ws","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=144","title":{"rendered":"Hello World with JAX-WS"},"content":{"rendered":"<p><strong>Create Eclipse project<\/strong><br \/>\n-Start Eclipse<br \/>\n-Create a new Java Project named helloworld.<br \/>\n-Add all jars in jaxws-ri (ref implementation) lib directory to project Libraries.<\/p>\n<p><strong>Server Implementation<\/strong><br \/>\n-Create a new java package named <strong>hello.server<\/strong><br \/>\n-Create a new class named HelloworldImpl. Annotate class with <strong>@WebService<\/strong><br \/>\n-Create a new method <strong>String sayHello(String hello)<\/strong><br \/>\n-Create a new class name <strong>HelloworldException extends Exception<\/strong><\/p>\n<pre lang=\"java\">\r\npackage hello.server;\r\n\r\nimport javax.jws.WebService;\r\n\r\n@WebService\r\npublic class HelloworldImpl {\r\n\r\n  public String sayHello(String hello) \r\n    throws HelloworldException{\r\n    System.out.printf(\"Prepare to say: %s\\n\", hello);\r\n    if (hello.trim().length() < 1)\r\n      throw new HelloworldException(\r\n        \"Hello World Error.\", \r\n        \"You need to provide a string\");\r\n    return String.format(\"Hello, %s, from jax-ws!\\n\", hello);\r\n  }\r\n}\r\n<\/pre>\n<p><strong>Build Server with Ant<\/strong><br \/>\n-Setup environment variables<br \/>\nJAVA_HOME<br \/>\nCATALINA_HOME<br \/>\nJAXWS_HOME<br \/>\n-Create a new Ant build file named build.xml<br \/>\n-Setup jaxws classpath<\/p>\n<pre lang=\"xml\"><path id=\"jaxws.classpath\">\r\n  <pathelement location=\"${java.home}\/..\/lib\/tools.jar\"\/>\r\n  <fileset dir=\"${lib.home}\">\r\n    <include name=\"*.jar\"\/>\r\n    <exclude name=\"j2ee.jar\"\/>\r\n  <\/fileset>\r\n<\/path>\r\n<\/pre>\n<p>-Import ant tasks<\/p>\n<pre lang=\"xml\"><taskdef name=\"apt\" \r\n  classname=\"com.sun.tools.ws.ant.Apt\">\r\n  <classpath refid=\"jaxws.classpath\"\/>\r\n<\/taskdef>\r\n\r\n<taskdef name=\"wsimport\" \r\n  classname=\"com.sun.tools.ws.ant.WsImport\">\r\n  <classpath refid=\"jaxws.classpath\"\/>\r\n<\/taskdef><\/pre>\n<p><strong>Generate server side java code<\/strong><\/p>\n<pre lang=\"xml\"><target name=\"generate-server\" depends=\"setup\">\r\n  <apt\r\n    fork=\"true\"\r\n    debug=\"true\"\r\n    verbose=\"true\"\r\n    destdir=\"${build.classes.home}\"\r\n    sourcedestdir=\"${basedir}\/src\"\r\n    sourcepath=\"${basedir}\/src\">\r\n    <classpath>\r\n      <path refid=\"jaxws.classpath\"\/>\r\n      <pathelement location=\"${basedir}\/src\"\/>\r\n    <\/classpath>\r\n    <option key=\"r\" value=\"${build.home}\"\/>\r\n    <source dir=\"${basedir}\/src\">\r\n      <include name=\"**\/server\/*.java\"\/>\r\n    <\/source>\r\n  <\/apt>\r\n<\/target><\/pre>\n<p>-This generates three java sources in the hello.server.jaxws pacakge<br \/>\n<strong>SayHello.java<br \/>\nSayHelloResponse.java<br \/>\nHelloworldExceptionBean.java<br \/>\n<\/strong><br \/>\n<strong>Create war file<\/strong><\/p>\n<pre lang=\"xml\"><target name=\"create-war\">\r\n  <war warfile=\"${build.war.home}\/jaxws-${ant.project.name}.war\" \r\n    webxml=\"web.xml\">\r\n    <webinf dir=\"${basedir}\" \r\n      includes=\"sun-jaxws.xml\"\/>\r\n    <classes dir=\"${build.classes.home}\"\/>\r\n  <\/war>\r\n<\/target><\/pre>\n<p>-web.xml<\/p>\n<pre lang=\"xml\">\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<web-app version=\"2.4\" \r\n  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 \r\n  http:\/\/java.sun.com\/xml\/ns\/j2ee\/web-app_2_4.xsd\">\r\n  <description>hello world from jaxws<\/description>\r\n  <display-name>hello world<\/display-name>\r\n  <listener>\r\n    <listener-class>com.sun.xml.ws.transport.http.servlet\r\n    .WSServletContextListener<\/listener-class>\r\n  <\/listener>\r\n  <servlet>\r\n    <description>Hello world from jaxws<\/description>\r\n    <display-name>helloworld<\/display-name>\r\n    <servlet-name>helloworld<\/servlet-name>\r\n    <servlet-class>com.sun.xml.ws.transport.http.servlet\r\n    .WSServlet<\/servlet-class>\r\n    <load-on-startup>1<\/load-on-startup>\r\n  <\/servlet>\r\n  <servlet-mapping>\r\n    <servlet-name>helloworld<\/servlet-name>\r\n    <url-pattern>\/sayHello<\/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>-sun-jaxws.xml<\/p>\n<pre lang=\"xml\"><?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<endpoints \r\n  xmlns='http:\/\/java.sun.com\/xml\/ns\/jax-ws\/ri\/runtime' \r\n  version='2.0'>\r\n  <endpoint\r\n    name='helloworld'\r\n    implementation='hello.server.HelloworldImpl'\r\n    url-pattern='\/sayHello'\/>\r\n<\/endpoints><\/pre>\n<p><strong>Deploy war to Tomcat 5.x<\/strong><\/p>\n<pre lang=\"xml\"><target name=\"deploy-tomcat\">\r\n  <copy \r\n    file=\"${build.war.home}\/jaxws-${ant.project.name}.war\"\r\n    todir=\"${env.CATALINA_HOME}\/webapps\"\r\n    overwrite=\"true\"\/>\r\n<\/target><\/pre>\n<p><strong>Test Hello World Web Service<\/strong><br \/>\n-Might need to restart Tomcat<br \/>\n-Point browser to http:\/\/localhost:8080\/jaxws-helloworld\/sayHello for web service summary page<br \/>\n-Point browser to http:\/\/localhost:8080\/jaxws-helloworld\/sayHello?wsdl for wsdl page<\/p>\n<p><strong>Client Implementation<\/strong><br \/>\n-Create a new package named hello.client<br \/>\n-Generate Client java codes from wsdl with Ant tool<\/p>\n<pre lang=\"xml\">\r\n<target name=\"generate-client\" depends=\"setup\">\r\n  <wsimport\r\n    debug=\"true\"\r\n    verbose=\"true\"\r\n    keep=\"true\"\r\n    destdir=\"${build.classes.home}\"\r\n    sourcedestdir=\"${basedir}\/src\"\r\n      package=\"hello.client\"\r\n      wsdl=\"http:\/\/localhost:8080\/jaxws-helloworld\/sayHello?wsdl\">\r\n  <\/wsimport>\r\n<\/target>\r\n<\/pre>\n<p>This generates seven java files from wsdl<br \/>\n<strong>HelloworldException.java<br \/>\nHelloworldException_Exception.java<br \/>\nHelloworldImpl.java<br \/>\nHelloworldImplService.java<br \/>\nObjectFactory.java<br \/>\nSayHello.java<br \/>\nSayHelloResponse.java<\/strong><\/p>\n<p>-Create a new class name HelloworldClient.java<\/p>\n<pre lang=\"java\">\r\npackage hello.client;\r\n\r\npublic class HelloworldClient {\r\n\r\n  public static void main(String[] args){\r\n    try{\r\n      HelloworldImpl port = new HelloworldImplService()\r\n        .getHelloworldImplPort();\r\n      String helloStr = \"JAX-WS\";\r\n      sayHello(port, helloStr);\r\n      helloStr = \"\";\r\n      sayHello(port, helloStr);\r\n    }catch(HelloworldException_Exception e){\r\n      System.out.printf(\"HelloworldException_Exception: %s\", \r\n          e.getFaultInfo().getDetail());\r\n    }\r\n  }\r\n\r\n  private static void sayHello(HelloworldImpl port, String helloStr) \r\n    throws HelloworldException_Exception {\r\n    String hello = port.sayHello(helloStr);\r\n    System.out.printf(\"Say hello to %s returns: %s\", helloStr, hello);\r\n  }\r\n}\r\n<\/pre>\n<p>-Run HelloworldClient give:<br \/>\nSay hello to JAX-WS returns: Hello, JAX-WS, from jax-ws!<br \/>\nHelloworldException_Exception: You need to provide a string<\/p>\n<p><a href='https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2008\/10\/jaxws-helloworld.zip' title='Say Hello World with JAX-WS'>Source Code<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Create Eclipse project -Start Eclipse -Create a new Java Project named helloworld. -Add all jars in jaxws-ri (ref implementation) lib directory to project Libraries. Server Implementation -Create a new java package named hello.server -Create a new class named HelloworldImpl. Annotate &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=144\">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":[14,41,22],"tags":[],"class_list":["post-144","post","type-post","status-publish","format-standard","hentry","category-java","category-jax-ws","category-soa"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-2k","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/144","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=144"}],"version-history":[{"count":4,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/144\/revisions"}],"predecessor-version":[{"id":2183,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/144\/revisions\/2183"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}