웹사이트 검색

Spring REST XML 및 JSON 예제


Spring Restful Web Services XML 및 JSON 예제에 오신 것을 환영합니다. 언젠가 나는 Spring REST JSON에 대한 기사를 썼고 XML을 지원하도록 프로그램을 변경하는 방법을 묻는 많은 의견을 받았습니다. 응용 프로그램이 XML과 JSON을 모두 지원하도록 만드는 방법을 묻는 이메일도 받았습니다.

스프링 REST XML 및 JSON

XML을 지원하도록 기존 애플리케이션을 얼마나 쉽게 확장할 수 있는지 보여줄 Spring REST XML 및 JSON 애플리케이션에 대한 기사를 작성하려고 생각했습니다. 기존 프로젝트를 변경할 예정이므로 먼저 아래 링크에서 다운로드하십시오.

Spring Restful Webservice 프로젝트 다운로드

이제 Spring Bean 구성 파일을 다음과 같이 변경하십시오.

  1. Define a bean of type Jaxb2RootElementHttpMessageConverter.

    <beans:bean id="xmlMessageConverter" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter">
    </beans:bean>
    
  2. Add above configured bean to RequestMappingHandlerAdapter property 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>
    

위의 변경 후 최종 스프링 빈 구성 파일은 아래와 같습니다. 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{

//no change in code
}