{"id":5045,"date":"2017-06-03T20:17:51","date_gmt":"2017-06-03T20:17:51","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5045"},"modified":"2017-06-03T20:17:51","modified_gmt":"2017-06-03T20:17:51","slug":"spring-configuration-for-a-jsonxml-rest-service-in-java","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/spring-configuration-for-a-jsonxml-rest-service-in-java\/","title":{"rendered":"Spring configuration for a JSON\/XML REST service in Java"},"content":{"rendered":"<p>In <a href=\"http:\/\/putridparrot.com\/blog\/creating-a-cxf-service-that-responds-with-json-or-xml\/\" target=\"_blank\">Creating a CXF service that responds with JSON or XML<\/a> and <a href=\"http:\/\/putridparrot.com\/blog\/creating-a-cxf-client-which-can-get-json-or-xml\/\" target=\"_blank\">Creating a CXF client which can get JSON or XML<\/a> we created a service and a client application for interacting with REST requests using XML or JSON as the body for the messages.<\/p>\n<p>The Java guys love spring and beans, my code was all in Java source. So I&#8217;m going to show how to convert both the server and client into spring configurations.<\/p>\n<p>In your client\/server code add a resources folder to \/src\/main and within that add a new xml file, named whatever you like, mine&#8217;s spring-client.xml and spring-server.xml (for the client and server respectively).<\/p>\n<p><strong>Changes to the server<\/strong><\/p>\n<p>Let&#8217;s concentrate on the server first &#8211; our code looked like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nJAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();\r\nfactoryBean.setResourceClasses(SampleServiceImpl.class);\r\nfactoryBean.setResourceProvider(new SingletonResourceProvider(new SampleServiceImpl()));\r\n\r\nMap&lt;Object, Object&gt; extensionMappings = new HashMap&lt;Object, Object&gt;();\r\nextensionMappings.put(&quot;xml&quot;, MediaType.APPLICATION_XML);\r\nextensionMappings.put(&quot;json&quot;, MediaType.APPLICATION_JSON);\r\nfactoryBean.setExtensionMappings(extensionMappings);\r\n\r\nList&lt;Object&gt; providers = new ArrayList&lt;Object&gt;();\r\nproviders.add(new JAXBElementProvider());\r\nproviders.add(new JacksonJsonProvider());\r\nfactoryBean.setProviders(providers);\r\n\r\nfactoryBean.setAddress(&quot;http:\/\/localhost:9000\/&quot;);\r\nServer server = factoryBean.create();\r\n<\/pre>\n<p>The first thing we need to do is update our pom.xml to import spring, so add the following to the properties section<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;spring.version&gt;2.5.6&lt;\/spring.version&gt;\r\n<\/pre>\n<p>and then these dependencies<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\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;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<\/pre>\n<p>Now change the Java code in our main method, replacing all the code which is listed above, to <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nApplicationContext context = new ClassPathXmlApplicationContext(&quot;spring-server.xml&quot;);\r\n<\/pre>\n<p>Now we need to add the following to our spring-server.xml file<\/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:jaxrs=&quot;http:\/\/cxf.apache.org\/jaxrs&quot;\r\n       xsi:schemaLocation=&quot;\r\n         http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans-2.0.xsd\r\n         http:\/\/cxf.apache.org\/jaxrs http:\/\/cxf.apache.org\/schemas\/jaxrs.xsd&quot;&gt;\r\n\r\n    &lt;jaxrs:server address=&quot;http:\/\/localhost:9000&quot;&gt;\r\n        &lt;jaxrs:serviceBeans&gt;\r\n            &lt;bean class=&quot;SampleServiceImpl&quot; \/&gt;\r\n        &lt;\/jaxrs:serviceBeans&gt;\r\n        &lt;jaxrs:extensionMappings&gt;\r\n            &lt;entry key=&quot;json&quot; value=&quot;application\/json&quot;\/&gt;\r\n            &lt;entry key=&quot;xml&quot; value=&quot;application\/xml&quot;\/&gt;\r\n        &lt;\/jaxrs:extensionMappings&gt;\r\n        &lt;jaxrs:providers&gt;\r\n            &lt;bean class=&quot;org.apache.cxf.jaxrs.provider.JAXBElementProvider&quot; \/&gt;\r\n            &lt;bean class=&quot;org.codehaus.jackson.jaxrs.JacksonJsonProvider&quot; \/&gt;\r\n        &lt;\/jaxrs:providers&gt;\r\n    &lt;\/jaxrs:server&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p>We&#8217;re using the jaxrs configuration to create our service beans which means the server is automatically created for us. <\/p>\n<p>That&#8217;s it for the server.<\/p>\n<p><strong>Changes to the client<\/strong><\/p>\n<p>Let&#8217;s look at the existing client code<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nList&lt;Object&gt; providers = new ArrayList&lt;Object&gt;();\r\nproviders.add(new JAXBElementProvider());\r\nproviders.add(new JacksonJsonProvider());\r\n\r\nSampleService service = JAXRSClientFactory.create(\r\n   &quot;http:\/\/localhost:9000&quot;, SampleService.class, providers);\r\n\r\nWebClient.client(service)\r\n   .type(MediaType.APPLICATION_JSON_TYPE)\r\n   .accept(MediaType.APPLICATION_JSON_TYPE);\r\n<\/pre>\n<p>First off, we need to add spring to the pom.xml as we needed for the server, so duplicate the steps for updating the pom.xml as already outlined.<\/p>\n<p>Next we replace our client code (above) with the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nApplicationContext context = new ClassPathXmlApplicationContext(&quot;spring-client.xml&quot;);\r\nSampleService service = (SampleService)context.getBean(&quot;client&quot;);\r\n<\/pre>\n<p>and finally put the following into the spring-client.xml file<\/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:jaxrs=&quot;http:\/\/cxf.apache.org\/jaxrs&quot;\r\n       xsi:schemaLocation=&quot;\r\n         http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans-2.0.xsd\r\n         http:\/\/cxf.apache.org\/jaxrs http:\/\/cxf.apache.org\/schemas\/jaxrs.xsd&quot;&gt;\r\n\r\n    &lt;jaxrs:client id=&quot;client&quot; address=&quot;http:\/\/localhost:9000&quot; serviceClass=&quot;SampleService&quot;&gt;\r\n        &lt;jaxrs:providers&gt;\r\n            &lt;bean class=&quot;org.apache.cxf.jaxrs.provider.JAXBElementProvider&quot; \/&gt;\r\n            &lt;bean class=&quot;org.codehaus.jackson.jaxrs.JacksonJsonProvider&quot; \/&gt;\r\n        &lt;\/jaxrs:providers&gt;\r\n        &lt;jaxrs:headers&gt;\r\n            &lt;entry key=&quot;Accept&quot; value=&quot;application\/json&quot; \/&gt;\r\n        &lt;\/jaxrs:headers&gt;\r\n    &lt;\/jaxrs:client&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p>and that&#8217;s it &#8211; run the server, then run the client and all should work.<\/p>\n<p><em>Note: I&#8217;ll leave it to the reader to add the relevant import statements<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Creating a CXF service that responds with JSON or XML and Creating a CXF client which can get JSON or XML we created a service and a client application for interacting with REST requests using XML or JSON as the body for the messages. The Java guys love spring and beans, my code was [&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":[161,164,162],"tags":[],"class_list":["post-5045","post","type-post","status-publish","format-standard","hentry","category-java","category-rest","category-spring"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5045","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=5045"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5045\/revisions"}],"predecessor-version":[{"id":5078,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5045\/revisions\/5078"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}