{"id":4993,"date":"2017-05-29T13:00:11","date_gmt":"2017-05-29T13:00:11","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4993"},"modified":"2017-05-29T13:00:11","modified_gmt":"2017-05-29T13:00:11","slug":"creating-a-cxf-client-which-can-get-json-or-xml","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/creating-a-cxf-client-which-can-get-json-or-xml\/","title":{"rendered":"Creating a CXF client which can get JSON or XML"},"content":{"rendered":"<p>In the previous post we created a server which serves up JSON or XML via a REST style interface and whilst we also demonstrated calling it from a browser, we really now need to look at implementing a compatible client.<\/p>\n<p><strong>POM first<\/strong><\/p>\n<p>Create a pom.xml and copy the following into it (within your client project folder)<\/p>\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.client&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;ppclient&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;ppclient&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.7.18&lt;\/cxf.version&gt;\r\n        &lt;httpclient.version&gt;4.3.6&lt;\/httpclient.version&gt;\r\n        &lt;surefire.version&gt;2.19.1&lt;\/surefire.version&gt;\r\n        &lt;jackson.version&gt;1.9.13&lt;\/jackson.version&gt;\r\n    &lt;\/properties&gt;\r\n\r\n    &lt;dependencies&gt;\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        &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        &lt;dependency&gt;\r\n            &lt;groupId&gt;org.apache.httpcomponents&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;httpclient&lt;\/artifactId&gt;\r\n            &lt;version&gt;${httpclient.version}&lt;\/version&gt;\r\n            &lt;exclusions&gt;\r\n                &lt;exclusion&gt;\r\n                    &lt;artifactId&gt;commons-logging&lt;\/artifactId&gt;\r\n                    &lt;groupId&gt;commons-logging&lt;\/groupId&gt;\r\n                &lt;\/exclusion&gt;\r\n            &lt;\/exclusions&gt;\r\n        &lt;\/dependency&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;org.codehaus.jackson&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;jackson-jaxrs&lt;\/artifactId&gt;\r\n            &lt;version&gt;${jackson.version}&lt;\/version&gt;\r\n        &lt;\/dependency&gt;\r\n    &lt;\/dependencies&gt;\r\n&lt;\/project&gt;\r\n<\/pre>\n<p>Now we create our folder structure, so create the following from the root folder<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nsrc\/main\/java\r\n<\/pre>\n<p>Select the java folder in IntelliJ and Mark Directory As Sources Root.<\/p>\n<p>We also need to copy the previously generated Java files (via XJC) into our java folder.<\/p>\n<p>Now we&#8217;re going to use a proxy to access our service, but we&#8217;ll still need a representation of the service. This is another good reason why we separated the interface for the service from the implementation, so copy the interface from the server into our java folder (here&#8217;s the code)<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\r\nimport com.putridparrot.sample.PurchaseOrderType;\r\n\r\nimport javax.ws.rs.GET;\r\nimport javax.ws.rs.Path;\r\nimport javax.ws.rs.Produces;\r\nimport javax.ws.rs.core.MediaType;\r\nimport javax.xml.bind.JAXBElement;\r\n\r\n@Path(&quot;service&quot;)\r\n@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})\r\npublic interface SampleService {\r\n    @GET\r\n    @Path(&quot;purchaseOrder&quot;)\r\n    public PurchaseOrderType getPurchaseOrder();\r\n}\r\n<\/pre>\n<p>Nothing new here, so let&#8217;s move onto the actual client code. Add a new class, mine&#8217;s ClientApplication and it&#8217;s listed below<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport com.putridparrot.sample.PurchaseOrderType;\r\nimport org.apache.cxf.jaxrs.client.JAXRSClientFactory;\r\nimport org.apache.cxf.jaxrs.client.WebClient;\r\nimport org.apache.cxf.jaxrs.provider.JAXBElementProvider;\r\nimport org.codehaus.jackson.jaxrs.JacksonJsonProvider;\r\n\r\nimport javax.ws.rs.core.MediaType;\r\nimport javax.xml.bind.JAXBElement;\r\nimport java.io.IOException;\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\n\r\npublic class ClientApplication {\r\n    public static void main(String&#x5B;] args) throws IOException {\r\n\r\n        List&lt;Object&gt; providers = new ArrayList&lt;Object&gt;();\r\n        providers.add(new JAXBElementProvider());\r\n        providers.add(new JacksonJsonProvider());\r\n\r\n        SampleService service = JAXRSClientFactory.create(\r\n           &quot;http:\/\/localhost:9000&quot;, \r\n           SampleService.class, \r\n           providers);\r\n\r\n        WebClient.client(service)\r\n           .type(MediaType.APPLICATION_XML_TYPE)\r\n           .accept(MediaType.APPLICATION_XML_TYPE);\r\n\r\n        PurchaseOrderType request = service.getPurchaseOrder();\r\n\r\n        System.out.println(request.getBillTo().getName());\r\n        System.in.read();\r\n    }\r\n}\r\n<\/pre>\n<p>Now, in the above we&#8217;re again supplying providers for the different types, but this is solely for us to test switching between them, if you&#8217;re solely interested in XML then remove the providers, but you&#8217;ll still need to specify XML as the accept type.<\/p>\n<p>To switch to JSON as the type returned, we simply switch replace the WebClient code with<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nWebClient.client(service)\r\n   .type(MediaType.APPLICATION_JSON_TYPE)\r\n   .accept(MediaType.APPLICATION_JSON_TYPE);\r\n<\/pre>\n<p>Ofcourse, in reality we don&#8217;t care too much about the format of the message returned when using it with a proxy unless we&#8217;re constrained by the server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous post we created a server which serves up JSON or XML via a REST style interface and whilst we also demonstrated calling it from a browser, we really now need to look at implementing a compatible client. POM first Create a pom.xml and copy the following into it (within your client project [&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],"tags":[],"class_list":["post-4993","post","type-post","status-publish","format-standard","hentry","category-cxf","category-java"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4993","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=4993"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4993\/revisions"}],"predecessor-version":[{"id":5018,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4993\/revisions\/5018"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}