{"id":4707,"date":"2017-03-23T22:19:37","date_gmt":"2017-03-23T22:19:37","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4707"},"modified":"2017-03-23T22:19:37","modified_gmt":"2017-03-23T22:19:37","slug":"writing-the-hello-world-cxf-webservice","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/writing-the-hello-world-cxf-webservice\/","title":{"rendered":"Writing the Hello World CXF webservice"},"content":{"rendered":"<p>Before I get into this post, I&#8217;m well aware that SOAP hasn&#8217;t been the cool kid on the block in terms of web service messaging for a while, but I&#8217;m looking at updating an old application&#8217;s webservice from a framework which is no longer supported on an older version of Java to a supported framework. So we need to keep everything working using SOAP.<\/p>\n<p>Following these steps, we can get a service up and running in next to no time (ofcourse it took me far longer than &#8220;no time&#8221; to find out how to do this :)).<\/p>\n<ul>\n<li>Create yourself a folder for your project, mine&#8217;s cxftest<\/li>\n<li>Within your project&#8217;s folder create a Maven pom.xml to grab all our required dependencies we&#8217;ll require &#8211; here&#8217;s the pom\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;project xmlns=&quot;http:\/\/maven.apache.org\/POM\/4.0.0&quot; xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n  xsi:schemaLocation=&quot;http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/maven-v4_0_0.xsd&quot;&gt;\r\n  &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\r\n  &lt;groupId&gt;com.putridparrot.core&lt;\/groupId&gt;\r\n  &lt;artifactId&gt;cxftest&lt;\/artifactId&gt;\r\n  &lt;packaging&gt;jar&lt;\/packaging&gt;\r\n  &lt;version&gt;1.0-SNAPSHOT&lt;\/version&gt;\r\n  &lt;name&gt;cxftest&lt;\/name&gt;\r\n  &lt;url&gt;http:\/\/maven.apache.org&lt;\/url&gt;\r\n\r\n  &lt;properties&gt;\r\n    &lt;cxf.version&gt;2.2.3&lt;\/cxf.version&gt;\r\n  &lt;\/properties&gt;\r\n\r\n  &lt;dependencies&gt;\r\n\r\n    &lt;dependency&gt;\r\n      &lt;groupId&gt;org.apache.cxf&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;cxf-rt-frontend-jaxws&lt;\/artifactId&gt;\r\n      &lt;version&gt;${cxf.version}&lt;\/version&gt;\r\n    &lt;\/dependency&gt;\r\n\r\n    &lt;dependency&gt;\r\n      &lt;groupId&gt;org.apache.cxf&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;cxf-rt-transports-http&lt;\/artifactId&gt;\r\n      &lt;version&gt;${cxf.version}&lt;\/version&gt;\r\n    &lt;\/dependency&gt;\r\n\r\n    &lt;dependency&gt;\r\n      &lt;groupId&gt;junit&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;junit&lt;\/artifactId&gt;\r\n      &lt;version&gt;3.8.1&lt;\/version&gt;\r\n      &lt;scope&gt;test&lt;\/scope&gt;\r\n    &lt;\/dependency&gt;\r\n  &lt;\/dependencies&gt;\r\n&lt;\/project&gt;\r\n<\/pre>\n<\/li>\n<li>We&#8217;re going to do everything from scratch here, but continue with the layout that Maven might generate for us, so off of your project&#8217;s folder create src\/main\/java\/putridparrot\/core<\/li>\n<li>Now add three files, App.java, HelloWorld.java and HelloWorldImpl.java<\/li>\n<li>Paste the following code into HelloWorld.java\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.putridparrot.core;\r\n\r\nimport javax.jws.WebParam;\r\nimport javax.jws.WebService;\r\n\r\n@WebService\r\npublic interface HelloWorld {\r\n    String reply(@WebParam(name=&quot;text&quot;) String text);\r\n}\r\n<\/pre>\n<\/li>\n<li>Now let&#8217;s paste the following into the HelloWorldImpl\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.putridparrot.core;\r\n\r\nimport javax.jws.WebParam;\r\nimport javax.jws.WebService;\r\n\r\n@WebService(endpointInterface = &quot;com.putridparrot.core.HelloWorld&quot;, serviceName=&quot;HelloWorld&quot;)\r\npublic class HelloWorldImpl implements HelloWorld {\r\n    public String reply(@WebParam(name = &quot;text&quot;) String text) {\r\n        return &quot;Hello &quot; + text;\r\n    }\r\n}\r\n<\/pre>\n<\/li>\n<li>Finally paste the following into App.java\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.putridparrot.core;\r\n\r\nimport javax.xml.ws.Endpoint;\r\nimport java.io.IOException;\r\n\r\npublic class App {\r\n   public static void main( String&#x5B;] args ) \r\n      throws IOException, InterruptedException  {\r\n\r\n   HelloWorldImpl impl = new HelloWorldImpl();\r\n   String address = &quot;http:\/\/localhost:9000\/HelloWorld&quot;;\r\n   Endpoint.publish(address, impl);\r\n\r\n   \/\/ hold the service open\r\n   System.in.read();\r\n\r\n   System.exit(0);\r\n}\r\n<\/pre>\n<\/li>\n<\/ul>\n<p>As you&#8217;ve probably noticed, we&#8217;re not using Spring to create our webservice (the HelloWorldImpl) and thus there&#8217;s no spring configuration or the likes. This really is a bare bones implementation to get us started.<\/p>\n<p>Now if you run this you should be able to use the URL http:\/\/localhost:9000\/HelloWorld?wsdl to grab the definition meta data. Hence from a C# client you can use wsdl.exe or using Visual Studio&#8217;s Add Service reference to generate C# classes to use this service.<\/p>\n<p><strong>Alternate code to start the server<\/strong><\/p>\n<p>In the above we used Endpoint to create the server, the alternative is to use the JaxWsServerFactoryBean, here&#8217;s the code<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nJaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();\r\nfactory.setServiceClass(HelloWorld.class);\r\nfactory.setAddress(&quot;http:\/\/localhost:9000\/HelloWorld&quot;);\r\nfactory.create();\r\n<\/pre>\n<p><strong>Beanify it<\/strong><\/p>\n<p>Okay, so we&#8217;ve proved the service works at this point, but we really don&#8217;t want to hardcode the service like this, so it&#8217;s type to turn it into a Spring bean.<\/p>\n<p>First off let&#8217;s add to our pom.xml file, you&#8217;ll need the following added to the properties element<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;spring.version&gt;3.0.5.RELEASE&lt;\/spring.version&gt;\r\n<\/pre>\n<p>and then the following dependencies need to be added<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n    &lt;dependency&gt;\r\n      &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;spring-boot&lt;\/artifactId&gt;\r\n      &lt;version&gt;1.5.2.RELEASE&lt;\/version&gt;\r\n    &lt;\/dependency&gt;\r\n\r\n    &lt;dependency&gt;\r\n      &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;spring-boot-autoconfigure&lt;\/artifactId&gt;\r\n      &lt;version&gt;1.5.2.RELEASE&lt;\/version&gt;\r\n    &lt;\/dependency&gt;\r\n\r\n    &lt;dependency&gt;\r\n      &lt;groupId&gt;org.springframework&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;spring-core&lt;\/artifactId&gt;\r\n      &lt;version&gt;${spring.version}&lt;\/version&gt;\r\n    &lt;\/dependency&gt;\r\n\r\n    &lt;dependency&gt;\r\n      &lt;groupId&gt;org.springframework&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;spring-context&lt;\/artifactId&gt;\r\n      &lt;version&gt;${spring.version}&lt;\/version&gt;\r\n    &lt;\/dependency&gt;\r\n\r\n    &lt;dependency&gt;\r\n      &lt;groupId&gt;cglib&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;cglib&lt;\/artifactId&gt;\r\n      &lt;version&gt;2.2.2&lt;\/version&gt;\r\n    &lt;\/dependency&gt;\r\n<\/pre>\n<p>and we&#8217;re going to use Jetty as the HTTP server, so also add the following dependency<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;dependency&gt;\r\n   &lt;groupId&gt;org.apache.cxf&lt;\/groupId&gt;\r\n   &lt;artifactId&gt;cxf-rt-transports-http-jetty&lt;\/artifactId&gt;\r\n   &lt;version&gt;${cxf.version}&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n<\/pre>\n<p>Obviously you&#8217;ll need to run <em>mvn install<\/em> to get the dependencies updated.<\/p>\n<p>Next we need to create our beans file, within this we&#8217;ll actually create a server\/endpoint which will be executed. So create a resources folder off of src\/main in your project&#8217;s folder and add an XML file, mine&#8217;s named cxf.xml &#8211; within this file we can use one of either of these configurations<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n       xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n       xmlns:jaxws=&quot;http:\/\/cxf.apache.org\/jaxws&quot;\r\n       xsi:schemaLocation=&quot; http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd http:\/\/cxf.apache.org\/jaxws http:\/\/cxf.apache.org\/schemas\/jaxws.xsd&quot;&gt;\r\n    &lt;import resource=&quot;classpath:META-INF\/cxf\/cxf.xml&quot;\/&gt;\r\n    &lt;import resource=&quot;classpath:META-INF\/cxf\/cxf-extension-http.xml&quot; \/&gt;\r\n    &lt;import resource=&quot;classpath:META-INF\/cxf\/cxf-extension-soap.xml&quot; \/&gt;\r\n    &lt;import resource=&quot;classpath:META-INF\/cxf\/cxf-extension-http-jetty.xml&quot; \/&gt;\r\n\r\n    &lt;jaxws:server id=&quot;helloWorld&quot;  serviceClass=&quot;com.putridparrot.core.HelloWorld&quot; address=&quot;http:\/\/localhost:9000\/HelloWorld&quot;&gt;\r\n        &lt;jaxws:serviceBean&gt;\r\n            &lt;bean class=&quot;com.putridparrot.core.HelloWorldImpl&quot;\/&gt;\r\n        &lt;\/jaxws:serviceBean&gt;\r\n    &lt;\/jaxws:server&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p>or using an Endpoint we can use the following<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n       xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n       xmlns:jaxws=&quot;http:\/\/cxf.apache.org\/jaxws&quot;\r\n       xsi:schemaLocation=&quot; http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd http:\/\/cxf.apache.org\/jaxws http:\/\/cxf.apache.org\/schemas\/jaxws.xsd&quot;&gt;\r\n    &lt;import resource=&quot;classpath:META-INF\/cxf\/cxf.xml&quot;\/&gt;\r\n    &lt;import resource=&quot;classpath:META-INF\/cxf\/cxf-extension-http.xml&quot; \/&gt;\r\n    &lt;import resource=&quot;classpath:META-INF\/cxf\/cxf-extension-soap.xml&quot; \/&gt;\r\n    &lt;import resource=&quot;classpath:META-INF\/cxf\/cxf-extension-http-jetty.xml&quot; \/&gt;\r\n\r\n &lt;jaxws:endpoint id=&quot;helloWorld&quot; implementor=&quot;com.putridparrot.core.HelloWorldImpl&quot;\r\n                    address=&quot;http:\/\/localhost:9000\/HelloWorld&quot;&gt;\r\n        &lt;jaxws:features&gt;\r\n            &lt;bean class=&quot;org.apache.cxf.feature.LoggingFeature&quot;\/&gt;\r\n        &lt;\/jaxws:features&gt;\r\n    &lt;\/jaxws:endpoint&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p>You can change the address to <em>address=&#8221;\/HelloWorld&#8221;<\/em> if you are using the default port etc. Otherwise to remove this will require looking into overriding Jetty&#8217;s port etc. (which is outside the scope of this post).<\/p>\n<p>Finally we need to have the <em>main<\/em> method wire everything up, so within main we should have the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nnew ClassPathXmlApplicationContext(&quot;cxf.xml&quot;);\r\n\r\n\/\/ stop the server shutting down\r\nSystem.in.read();\r\nSystem.exit(0);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Before I get into this post, I&#8217;m well aware that SOAP hasn&#8217;t been the cool kid on the block in terms of web service messaging for a while, but I&#8217;m looking at updating an old application&#8217;s webservice from a framework which is no longer supported on an older version of Java to a supported framework. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[163,161,77],"tags":[],"class_list":["post-4707","post","type-post","status-publish","format-standard","hentry","category-cxf","category-java","category-soap"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=4707"}],"version-history":[{"count":9,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4707\/revisions"}],"predecessor-version":[{"id":4724,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4707\/revisions\/4724"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}