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 bean配置文件进行以下更改。
- 定义类型的bean
Jaxb2RootElementHttpMessageConverter
。<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="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://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.journaldev.spring.controller" />
</beans:beans>
我们知道,对于使用JAXB编组的类,我们需要使用注释对其进行@XmlRootElement
注释。所以将它添加到我们的Employee
模型类中。
Employee.java
@XmlRootElement
public class Employee implements Serializable{
//no change in code
}
就是这样,我们完成了。我们的Spring应用程序将同时支持JSON和XML。它甚至会支持带有JSON响应的XML请求,反之亦然。以下是一些显示此操作的屏幕截图。
注意:我正在使用Postman Chrome应用程序,您可以使用任何其他客户端进行此测试。
1. XML响应:确保将Accept标头作为“application / xml”传递。
2. JSON响应:确保将Accept标头作为“application / json”传递。
3. 带有JSON响应的XML请求:确保Accept标头是“application / json”,Content-Type标头是“text / xml”,如下图所示。
这就是支持XML和JSON的Spring Restful Web服务示例。您可以看到扩展Spring框架是多么容易,这是Spring框架流行的主要原因之一。