Esempio di Spring REST XML e JSON

Benvenuto nell’esempio di servizi Web Spring Restful XML e JSON. Tempo fa ho scritto un articolo su Spring REST JSON e ho ricevuto molti commenti che chiedevano come modificare il programma per supportare XML. Ho ricevuto anche alcune email che chiedevano come fare in modo che l’applicazione supportasse sia XML che JSON.

Spring REST XML e 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.

Scarica il progetto Spring Restful Webservice

Ora apporta le seguenti modifiche al file di configurazione del bean Spring.

  1. Definisci un bean di tipo Jaxb2RootElementHttpMessageConverter.

    <beans:bean id="xmlMessageConverter" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter">
    </beans:bean>
    
  2. Aggiungi il bean configurato sopra alla proprietà messageConverters di 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>
    

Dopo le modifiche sopra indicate, il nostro file di configurazione finale del bean di Spring avrà un aspetto simile al seguente. 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>

Sappiamo che per lavorare con la marshalling JAXB per una classe, è necessario annotarla con @XmlRootElement. Quindi aggiungi questo alla nostra classe del modello Employee. Employee.java

@XmlRootElement
public class Employee implements Serializable{

//nessuna modifica nel codice
}

Ecco, siamo FINITI. La nostra applicazione Spring supporterà sia JSON che XML. Supporterà persino richieste XML con risposta JSON e viceversa. Di seguito ci sono alcune schermate che mostrano questo in azione. NOTA: Sto utilizzando l’applicazione Chrome Postman per questo, puoi utilizzare qualsiasi client REST per questo test. 1. Risposta XML: Assicurati di passare l’intestazione Accept come “application/xml”. 2. Risposta JSON: Assicurati di passare l’intestazione Accept come “application/json”. 3. Richiesta XML con risposta JSON: Assicurati che l’intestazione Accept sia “application/json” e l’intestazione Content-Type sia “text/xml” come mostrato nelle immagini seguenti. Questo è tutto per l’esempio di servizi Web RESTful di Spring per il supporto sia di XML che di JSON. Puoi vedere quanto sia facile estendere il framework Spring, questa è una delle principali ragioni della popolarità del framework Spring.

Source:
https://www.digitalocean.com/community/tutorials/spring-rest-xml-and-json-example