欢迎来到Spring Restful Web Services XML和JSON示例。前段时间我写了一篇关于Spring REST JSON的文章,收到了很多评论,询问如何更改程序以支持XML。我也收到了一些邮件,询问如何使应用程序同时支持XML和JSON。
Spring REST XML和JSON
I thought to write an article about Spring REST XML and JSON application where I will show you how easily we can extend the existing application to support XML. Since I will be making changes to the existing project, make sure you first download it from below link.
现在按照以下步骤更改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.journaldev.spring.controller" />
</beans:beans>
我们知道,要使用 JAXB 进行类的编组,我们需要使用 @XmlRootElement
注解对其进行注解。因此,将其添加到我们的 Employee
模型类中。Employee.java
@XmlRootElement
public class Employee implements Serializable{
//代码无变化
}
搞定了,我们完成了。我们的Spring应用将支持JSON和XML。它甚至支持XML请求与JSON响应,反之亦然。以下是一些截图展示了它的工作方式。注意:我在使用Postman Chrome应用程序进行测试,你可以使用任何REST客户端进行测试。1. XML响应: 确保将Accept头设置为“application/xml”。 2. JSON响应: 确保将Accept头设置为“application/json”。
3. XML请求与JSON响应: 确保Accept头设置为“application/json”,Content-Type头设置为“text/xml”,如下图所示。
这就是Spring Restful web服务示例,支持XML和JSON。你可以看到扩展Spring框架有多么容易,这是Spring框架受欢迎的主要原因之一。
Source:
https://www.digitalocean.com/community/tutorials/spring-rest-xml-and-json-example