歡迎來到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。它甚至支持帶有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”,如下圖所示。
這就是關於Spring Restful Web服務支持XML和JSON的示例。您可以看到擴展Spring框架有多容易,這是Spring框架受歡迎的主要原因之一。
Source:
https://www.digitalocean.com/community/tutorials/spring-rest-xml-and-json-example