One example of Spring REST XML and JSON

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 XML and JSON can be reformulated as:
– Spring REST with support for both XML and JSON
– Spring REST that can handle XML and JSON formats.

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.

Please download the Spring Restful Webservice Project.

Make the specified modifications to the spring bean configuration file.
Create a bean of type Jaxb2RootElementHttpMessageConverter.

Include the previously configured bean in the messageConverters property of RequestMappingHandlerAdapter.

After making the aforementioned modifications, our ultimate configuration file for spring beans will appear as follows: servlet-context.xml.

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="https://www.springframework.org/schema/mvc"
	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="https://www.springframework.org/schema/beans"
	xmlns:context="https://www.springframework.org/schema/context"
	xsi:schemaLocation="https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	
	<!-- Configure to plugin JSON as request and response in method handler -->
	<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<beans:property name="messageConverters">
			<beans:list>
				<beans:ref bean="jsonMessageConverter"/>
				<beans:ref bean="xmlMessageConverter"/>
			</beans:list>
		</beans:property>
	</beans:bean>
	
	<!-- Configure bean to convert JSON to POJO and vice versa -->
	<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
	</beans:bean>	
	
	<beans:bean id="xmlMessageConverter" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter">
	</beans:bean>
	
	<context:component-scan base-package="com.scdev.spring.controller" />
	
</beans:beans>

In order to utilize JAXB marshalling for a class, it is necessary to annotate it with the @XmlRootElement annotation. Hence,

Employee model class, specifically Employee.java should include the @XmlRootElement annotation.

see more about Tutorial on how to set up a Hibernate Tomcat JNDI DataSource.(Opens in a new browser tab)

@XmlRootElement
public class Employee implements Serializable{

//no change in code
}

That’s it, we’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.

1. For XML responses, make sure to include the Accept header as “application/xml”.
2. For JSON responses, make sure to include the Accept header as “application/json”.
3. For XML requests with JSON responses, make sure the Accept header is “application/json” and the Content-Type header is “text/xml”, as shown in the screenshots.

That’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.

Leave a Reply 0

Your email address will not be published. Required fields are marked *