ברוכים הבאים לדוגמת 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 Restful Webservice
עכשיו עשה את השינויים הבאים בקובץ התצורה של יצירת הפול.
-
הגדר פול מסוג
Jaxb2RootElementHttpMessageConverter
.<beans:bean id="xmlMessageConverter" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"> </beans:bean>
-
הוסף את הפול שהוגדר לעיל למאפיין
messageConverters
שלRequestMappingHandlerAdapter
.<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>
לאחר השינויים האמורים לעיל, הקובץ הסופי של התצורה של ה-Bean של Spring שלנו ייראה כמו בהמשך. 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>
אנו יודעים שכדי לעבוד עם Marshalling של JAXB עבור מחלקה, עלינו לסמן אותה באמצעות ההערה @XmlRootElement
. לכן הוסף זאת למחלקת ה-Model שלנו Employee
. Employee.java
@XmlRootElement
public class Employee implements Serializable{
//אין שינוי בקוד
}
זהו, סיימנו. יישום האביב שלנו יתמוך גם ב־JSON וגם ב־XML. הוא יתמוך אפילו בבקשת XML עם תגובת JSON ולהיפך. למטה כמה תמונות מסך המציגות זאת בפועל. הערה: אני משתמש באפליקציית Postman בגרסת Chrome עבור זה, ניתן להשתמש בכל לקוח REST לבדיקה זו.
1. תגובת XML: ודא שאתה מעביר כותרת Accept כ"application/xml".
2. תגובת JSON: ודא שאתה מעביר כותרת Accept כ"application/json".
3. בקשת XML עם תגובת JSON: ודא שכותרת ה־Accept היא "application/json" וכותרת סוג התוכן היא "text/xml" כפי שמוצג בתמונות למטה.
זהו הכל עבור דוגמת שירותי ה־REST של Spring לתמיכה בשני המבנים, XML ו־JSON. ניתן לראות כמה קל להרחיב את מסגרת ה־Spring, זהו אחד מהסיבות העיקריות לפופולריותה של מסגרת Spring.
Source:
https://www.digitalocean.com/community/tutorials/spring-rest-xml-and-json-example