Spring REST XML和JSON示例

欢迎来到Spring Restful Web Services XML和JSON的示例。之前,我写了一篇关于Spring REST JSON的文章,收到了很多关于如何更改程序以支持XML的评论。还收到了一些电子邮件询问如何使应用程序同时支持XML和JSON。

Spring REST XML 和 JSON

我打算写一篇关于Spring REST XML和JSON应用的文章,我会向您展示如何轻松地扩展现有应用程序来支持XML。由于我将对现有项目进行更改,请确保您先从以下链接下载它。

下载Spring Restful Webservice项目

现在对Spring bean配置文件进行以下更改。

定义一个类型为Jaxb2RootElementHttpMessageConverter的bean。

<beans:bean id="xmlMessageConverter" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter">
</beans:bean>

将上述配置的bean添加到RequestMappingHandlerAdapter的messageConverters属性中。

<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>

在上述更改之后,我们的最终Spring Bean配置文件将如下所示。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.Olivia.spring.controller" />
	
</beans:beans>

我们知道,为了使用JAXB编组一个类,我们需要在它上面加上@XmlRootElement注解。所以将这个注解添加到我们的员工模型类Employee.java中。

@XmlRootElement
public class Employee implements Serializable{

//no change in code
}

这就是全部了,我们已经完成了。我们的Spring应用将支持JSON和XML两种格式。它甚至支持以XML请求并获取JSON响应,以及以JSON请求并获取XML响应。以下是一些展示这一功能的截图。注意:我在使用Postman Chrome应用程序进行测试,你可以使用任何REST客户端进行测试。 1. XML响应:确保将Accept头设置为“application/xml”。2. JSON响应:确保将Accept头设置为“application/json”。3. XML请求并获取JSON响应:确保Accept头设置为“application/json”,Content-Type头设置为“text/xml”,如下图所示。以上就是关于支持XML和JSON的Spring RESTful web服务示例的全部内容。你可以看到这么容易扩展Spring框架,这是Spring框架受欢迎的一个主要原因。