{"id":1139,"date":"2022-07-03T18:39:38","date_gmt":"2022-11-18T03:02:17","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/uncategorized\/one-example-of-spring-rest-xml-and-json\/"},"modified":"2024-03-03T04:34:12","modified_gmt":"2024-03-03T04:34:12","slug":"spring-rest-xml-and-json","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/","title":{"rendered":"One example of Spring REST XML and JSON"},"content":{"rendered":"<p>Greetings to the Spring Restful Web Services XML and JSON demonstration. In the past, I penned an article discussing Spring REST JSON, and in response, I received numerous inquiries on modifying the program to accommodate XML. Additionally, several individuals emailed me seeking guidance on enabling the application to handle both XML and JSON.<\/p>\n<h2>Spring REST XML and JSON can be reformulated as:<br \/>\n&#8211; Spring REST with support for both XML and JSON<br \/>\n&#8211; Spring REST that can handle XML and JSON formats.<\/h2>\n<p>I planned on writing an article about a Spring REST application that focuses on XML and JSON. In this article, I will demonstrate how simple it is to extend the current application to include XML support. Please ensure that you download the existing project from the provided link before I start making any modifications.<\/p>\n<p>Please download the Spring Restful Webservice Project.<\/p>\n<p>Make the specified modifications to the spring bean configuration file.<br \/>\nCreate a bean of type Jaxb2RootElementHttpMessageConverter.<\/p>\n<p>Include the previously configured bean in the messageConverters property of RequestMappingHandlerAdapter.<\/p>\n<p>After making the aforementioned modifications, our ultimate configuration file for spring beans will appear as follows: servlet-context.xml.<\/p>\n<pre class=\"post-pre\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;beans:beans xmlns=\"https:\/\/www.springframework.org\/schema\/mvc\"\r\n\txmlns:xsi=\"https:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n\txmlns:beans=\"https:\/\/www.springframework.org\/schema\/beans\"\r\n\txmlns:context=\"https:\/\/www.springframework.org\/schema\/context\"\r\n\txsi:schemaLocation=\"https:\/\/www.springframework.org\/schema\/mvc https:\/\/www.springframework.org\/schema\/mvc\/spring-mvc.xsd\r\n\t\thttps:\/\/www.springframework.org\/schema\/beans https:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\r\n\t\thttps:\/\/www.springframework.org\/schema\/context https:\/\/www.springframework.org\/schema\/context\/spring-context.xsd\"&gt;\r\n\r\n\t&lt;!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --&gt;\r\n\t\r\n\t&lt;!-- Enables the Spring MVC @Controller programming model --&gt;\r\n\t&lt;annotation-driven \/&gt;\r\n\r\n\t&lt;!-- Handles HTTP GET requests for \/resources\/** by efficiently serving up static resources in the ${webappRoot}\/resources directory --&gt;\r\n\t&lt;resources mapping=\"\/resources\/**\" location=\"\/resources\/\" \/&gt;\r\n\r\n\t&lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources in the \/WEB-INF\/views directory --&gt;\r\n\t&lt;beans:bean class=\"org.springframework.web.servlet.view.InternalResourceViewResolver\"&gt;\r\n\t\t&lt;beans:property name=\"prefix\" value=\"\/WEB-INF\/views\/\" \/&gt;\r\n\t\t&lt;beans:property name=\"suffix\" value=\".jsp\" \/&gt;\r\n\t&lt;\/beans:bean&gt;\r\n\t\r\n\t&lt;!-- Configure to plugin JSON as request and response in method handler --&gt;\r\n\t&lt;beans:bean class=\"org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter\"&gt;\r\n\t\t&lt;beans:property name=\"messageConverters\"&gt;\r\n\t\t\t&lt;beans:list&gt;\r\n\t\t\t\t&lt;beans:ref bean=\"jsonMessageConverter\"\/&gt;\r\n\t\t\t\t&lt;beans:ref bean=\"xmlMessageConverter\"\/&gt;\r\n\t\t\t&lt;\/beans:list&gt;\r\n\t\t&lt;\/beans:property&gt;\r\n\t&lt;\/beans:bean&gt;\r\n\t\r\n\t&lt;!-- Configure bean to convert JSON to POJO and vice versa --&gt;\r\n\t&lt;beans:bean id=\"jsonMessageConverter\" class=\"org.springframework.http.converter.json.MappingJackson2HttpMessageConverter\"&gt;\r\n\t&lt;\/beans:bean&gt;\t\r\n\t\r\n\t&lt;beans:bean id=\"xmlMessageConverter\" class=\"org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter\"&gt;\r\n\t&lt;\/beans:bean&gt;\r\n\t\r\n\t&lt;context:component-scan base-package=\"com.scdev.spring.controller\" \/&gt;\r\n\t\r\n&lt;\/beans:beans&gt;\r\n<\/code><\/pre>\n<p>In order to utilize JAXB marshalling for a class, it is necessary to annotate it with the @XmlRootElement annotation. Hence,<\/p>\n<p>Employee model class, specifically Employee.java should include the @XmlRootElement annotation.<\/p>\n<p>see more about <a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/tutorial-on-hibernate-tomcat-jndi-datasource\/\" target=\"_blank\" rel=\"noopener\">Tutorial on how to set up a Hibernate Tomcat JNDI DataSource.<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<pre class=\"post-pre\"><code>@XmlRootElement\r\npublic class Employee implements Serializable{\r\n\r\n\/\/no change in code\r\n}\r\n<\/code><\/pre>\n<p>That&#8217;s it, we&#8217;re finished. Our Spring application will now be compatible with both JSON and XML formats. It will even allow for XML requests with JSON responses, and vice versa. Here are some screenshots demonstrating this functionality. Please note that I am using the Postman Chrome application for this, but any REST client can be used for testing.<\/p>\n<p>1. For XML responses, make sure to include the Accept header as &#8220;application\/xml&#8221;.<br \/>\n2. For JSON responses, make sure to include the Accept header as &#8220;application\/json&#8221;.<br \/>\n3. For XML requests with JSON responses, make sure the Accept header is &#8220;application\/json&#8221; and the Content-Type header is &#8220;text\/xml&#8221;, as shown in the screenshots.<\/p>\n<p>That&#8217;s all for the example of Spring Restful web services supporting both XML and JSON. You can see how easy it is to expand the capabilities of the Spring framework, which is one of the main reasons for its popularity.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Greetings to the Spring Restful Web Services XML and JSON demonstration. In the past, I penned an article discussing Spring REST JSON, and in response, I received numerous inquiries on modifying the program to accommodate XML. Additionally, several individuals emailed me seeking guidance on enabling the application to handle both XML and JSON. Spring REST [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-1139","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>One example of Spring REST XML and JSON - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Spring REST XML and JSON can be reformulated as Spring REST with support for both XML and JSON\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"One example of Spring REST XML and JSON\" \/>\n<meta property=\"og:description\" content=\"Spring REST XML and JSON can be reformulated as Spring REST with support for both XML and JSON\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-18T03:02:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-03T04:34:12+00:00\" \/>\n<meta name=\"author\" content=\"Emily Johnson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Emily Johnson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/\"},\"author\":{\"name\":\"Emily Johnson\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378\"},\"headline\":\"One example of Spring REST XML and JSON\",\"datePublished\":\"2022-11-18T03:02:17+00:00\",\"dateModified\":\"2024-03-03T04:34:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/\"},\"wordCount\":401,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/\",\"name\":\"One example of Spring REST XML and JSON - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2022-11-18T03:02:17+00:00\",\"dateModified\":\"2024-03-03T04:34:12+00:00\",\"description\":\"Spring REST XML and JSON can be reformulated as Spring REST with support for both XML and JSON\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"One example of Spring REST XML and JSON\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378\",\"name\":\"Emily Johnson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g\",\"caption\":\"Emily Johnson\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/emilyjohnson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"One example of Spring REST XML and JSON - Blog - Silicon Cloud","description":"Spring REST XML and JSON can be reformulated as Spring REST with support for both XML and JSON","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/","og_locale":"en_US","og_type":"article","og_title":"One example of Spring REST XML and JSON","og_description":"Spring REST XML and JSON can be reformulated as Spring REST with support for both XML and JSON","og_url":"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2022-11-18T03:02:17+00:00","article_modified_time":"2024-03-03T04:34:12+00:00","author":"Emily Johnson","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Emily Johnson","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/"},"author":{"name":"Emily Johnson","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378"},"headline":"One example of Spring REST XML and JSON","datePublished":"2022-11-18T03:02:17+00:00","dateModified":"2024-03-03T04:34:12+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/"},"wordCount":401,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/","url":"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/","name":"One example of Spring REST XML and JSON - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2022-11-18T03:02:17+00:00","dateModified":"2024-03-03T04:34:12+00:00","description":"Spring REST XML and JSON can be reformulated as Spring REST with support for both XML and JSON","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"One example of Spring REST XML and JSON"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378","name":"Emily Johnson","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g","caption":"Emily Johnson"},"url":"https:\/\/www.silicloud.com\/blog\/author\/emilyjohnson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/1139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=1139"}],"version-history":[{"count":4,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/1139\/revisions"}],"predecessor-version":[{"id":1621,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/1139\/revisions\/1621"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=1139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=1139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=1139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}