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
    
    "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">

	
	
	
	<annotation-driven />

	
	<resources mapping="/resources/**" location="/resources/" />

	
	<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>
	
	
	<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>
	
	
	<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”传递。

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

Spring REST XML和JSON示例_第1张图片

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

Spring REST XML和JSON示例_第2张图片

Spring REST XML和JSON示例_第3张图片

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

你可能感兴趣的:(Spring)