{"id":4718,"date":"2017-03-23T22:41:07","date_gmt":"2017-03-23T22:41:07","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4718"},"modified":"2017-03-23T22:41:07","modified_gmt":"2017-03-23T22:41:07","slug":"hosting-more-than-one-service-using-cxf-and-jetty","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/hosting-more-than-one-service-using-cxf-and-jetty\/","title":{"rendered":"Hosting more than one service using CXF and Jetty"},"content":{"rendered":"<p>This is a logical &#8220;next post&#8221; to Hosting a CXF SOAP webservice in an existing Jetty instance.<\/p>\n<p>Let&#8217;s look at what&#8217;s need to host more than one service using CXF from a running instance of Jetty.<\/p>\n<p><strong>Changes to our SOAP Service<\/strong><\/p>\n<p>We&#8217;re going to do this by hosting our original HelloWorld service (from the previous post) with a single line change and alongside it we&#8217;ll create a REST service.<\/p>\n<p>So in the AppConfig from the previous post, rename getService to getSoapServer (or whatever you like) and the code should look like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Bean @DependsOn( &quot;cxf&quot; )\r\npublic Server getSoapServer() {\r\n   JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();\r\n   factory.setServiceBean(new HelloWorldImpl());\r\n   factory.setAddress(&quot;\/soap&quot;);\r\n   return factory.create();\r\n}\r\n<\/pre>\n<p>Apart from the method name change, we&#8217;ve added a setAddress call with a relative path. If you recall from the previous post our Jetty server will run on port 9000, i.e. http:\/\/localhost:9000 but now our SOAP service will be on http:\/\/localhost:9000\/soap\/HelloWorld?wsdl.<\/p>\n<p><strong>Adding a REST Service<\/strong><\/p>\n<p>Update the pom.xml with the following, in the properties we&#8217;ll add<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;json.version&gt;1.1.0-M1&lt;\/json.version&gt;\r\n&lt;glassfish.version&gt;1.0.4&lt;\/glassfish.version&gt;\r\n<\/pre>\n<p>and these dependencies<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;dependency&gt;\r\n   &lt;groupId&gt;javax.json&lt;\/groupId&gt;\r\n   &lt;artifactId&gt;javax.json-api&lt;\/artifactId&gt;\r\n   &lt;version&gt;${json.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-frontend-jaxrs&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-rs-extension-providers&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.glassfish&lt;\/groupId&gt;\r\n   &lt;artifactId&gt;javax.json&lt;\/artifactId&gt;\r\n   &lt;version&gt;${glassfish.version}&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n<\/pre>\n<p>Next we&#8217;ll create a simple REST service (I&#8217;ll actually duplicate the one from <a href=\"https:\/\/github.com\/reta\/jax-rs-2.0-spring-security\" target=\"_blank\">here<\/a>).<\/p>\n<p>So create a new Java class, mine&#8217;s named PeopleService and here&#8217;s the code<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.putridparrot.core;\r\n\r\nimport javax.json.JsonArray;\r\nimport javax.json.Json;\r\nimport javax.ws.rs.GET;\r\nimport javax.ws.rs.Path;\r\nimport javax.ws.rs.Produces;\r\n\r\n@Path(&quot;\/people&quot;)\r\npublic class PeopleService {\r\n    @Produces({&quot;application\/json&quot;})\r\n    @GET\r\n    public JsonArray getPeople() {\r\n        return Json.createArrayBuilder()\r\n                .add(Json.createObjectBuilder()\r\n                        .add(&quot;firstName&quot;, &quot;Brian&quot;)\r\n                        .add(&quot;lastName&quot;, &quot;Kernighan &quot;))\r\n                .add(Json.createObjectBuilder()\r\n                        .add(&quot;firstName&quot;, &quot;Dennis&quot;)\r\n                        .add(&quot;lastName&quot;, &quot;Ritchie &quot;))\r\n                .add(Json.createObjectBuilder()\r\n                        .add(&quot;firstName&quot;, &quot;Bjarne&quot;)\r\n                        .add(&quot;lastName&quot;, &quot;Stroustrup&quot;))\r\n                .build();\r\n    }\r\n}\r\n<\/pre>\n<p>within AppConfig, place the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Bean @DependsOn( &quot;cxf&quot; )\r\npublic Server getRestServer() {\r\n   JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();\r\n   factory.setServiceBean(new PeopleService());\r\n   factory.setProvider(new JsrJsonpProvider());\r\n   factory.setAddress(&quot;\/rest&quot;);\r\n   return factory.create();\r\n}\r\n<\/pre>\n<p>if you run the App.main, you should be able to navigate to http:\/\/localhost:9000\/soap\/HelloWorld?wsdl to get the SOAP service&#8217;s wsdl and http:\/\/localhost:9000\/rest\/people to get the results of the PeopleService call.<\/p>\n<p>Just to complete the picture &#8211; the PeopleService is given a path to the REST calls. We&#8217;re simply returning JSON and so we annotate with the @Produces line. As we&#8217;re only support a GET call to this service we annotate @GET. Within the getPeople method we build up a JSON document and that&#8217;s all there is to it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a logical &#8220;next post&#8221; to Hosting a CXF SOAP webservice in an existing Jetty instance. Let&#8217;s look at what&#8217;s need to host more than one service using CXF from a running instance of Jetty. Changes to our SOAP Service We&#8217;re going to do this by hosting our original HelloWorld service (from the previous [&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,164,77],"tags":[],"class_list":["post-4718","post","type-post","status-publish","format-standard","hentry","category-cxf","category-java","category-rest","category-soap"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4718","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=4718"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4718\/revisions"}],"predecessor-version":[{"id":4727,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4718\/revisions\/4727"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}