Spring REST XML和JSON示例

 

Spring REST XML和JSON示例

 

欢迎使用Spring Restful Web Services XML和JSON示例。有一段时间我写了一篇关于Spring REST JSON的文章,我收到很多评论,询问如何更改程序以支持XML。我收到了一些电子邮件,询问如何使应用程序同时支持XML和JSON。

Spring REST XML和JSON

我想写一篇关于Spring REST XML和JSON应用程序的文章,我将向您展示如何轻松扩展现有应用程序以支持XML。由于我将对现有项目进行更改,请确保首先从下面的链接下载它。

下载Spring Restful Webservice项目

现在对spring bean配置文件进行以下更改。

  1. 定义类型的bean Jaxb2RootElementHttpMessageConverter
    
    <beans:bean id="xmlMessageConverter" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter">
    </beans:bean>
    
  2. 将以上配置的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="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://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
}

就是这样,我们完成了。我们的Spring应用程序将同时支持JSON和XML。它甚至会支持带有JSON响应的XML请求,反之亦然。以下是一些显示此操作的屏幕截图。

注意:我正在使用Postman Chrome应用程序,您可以使用任何其他客户端进行此测试。

1. XML响应:确保将Accept标头作为“application / xml”传递。

spring xml,spring rest xml

2. JSON响应:确保将Accept标头作为“application / json”传递。

春天json,春天休息json

3. 带有JSON响应的XML请求:确保Accept标头是“application / json”,Content-Type标头是“text / xml”,如下图所示。

spring rest xml json,spring restful web services

春天宁静的网络服务

这就是支持XML和JSON的Spring Restful Web服务示例。您可以看到扩展Spring框架是多么容易,这是Spring框架流行的主要原因之一。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值