博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring REST XML和JSON示例
阅读量:2533 次
发布时间:2019-05-11

本文共 3145 字,大约阅读时间需要 10 分钟。

Welcome to Spring Restful Web Services XML and JSON example. Sometime back I wrote an article about and I got a lot of comments asking how to change the program to support XML. I got some emails also asking how to make application supports both XML as well as JSON.

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

Spring REST XML和JSON (Spring REST XML and 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 REST XML和JSON应用程序的文章,在这里我将向您展示如何轻松扩展现有应用程序以支持XML。 由于我将对现有项目进行更改,因此请确保首先从下面的链接下载它。

Now do following changes to spring bean configuration file.

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

  1. Define a bean of type Jaxb2RootElementHttpMessageConverter.

    定义类型为Jaxb2RootElementHttpMessageConverter的bean。
  2. Add above configured bean to RequestMappingHandlerAdapter property messageConverters.

    将上面配置的bean添加到RequestMappingHandlerAdapter属性messageConverters

After above changes, our final spring bean configuration file will look like below.

经过上述更改后,我们最终的spring bean配置文件将如下所示。

servlet-context.xml

servlet-context.xml

We know that for working with JAXB marshalling for a class, we need to annotate it with @XmlRootElement annotation. So add this to our Employee model class.

我们知道,对于使用JAXB编组的类,我们需要使用@XmlRootElement注释对其进行注释。 因此,将其添加到我们的Employee模型类中。

Employee.java

Employee.java

@XmlRootElementpublic class Employee implements Serializable{//no change in code}

That’s it, we are DONE. Our Spring application will support both JSON as well as XML. It will even support XML request with JSON response and vice versa. Below are some of the screenshots showing this in action.

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

NOTE: I am using Postman Chrome application for this, you can use any rest client for this testing.

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

1. XML Response: Make sure you pass Accept header as “application/xml”.

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

2. JSON Response: Make sure you pass Accept header as “application/json”.

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

3. XML Request with JSON Response: Make sure Accept header is “application/json” and Content-Type header is “text/xml” as shown in below images.

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

That’s all for Spring Restful web services example for supporting both XML and JSON. You can see how easy it is to extend Spring framework, this is one of the major reason of spring framework popularity.

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

翻译自:

转载地址:http://dtlzd.baihongyu.com/

你可能感兴趣的文章
Sybase IQ导出文件的几种方式
查看>>
案例:手动输入一个字符串,打散放进一个列表,小写字母反序 大写字母保持不变...
查看>>
linux 系统下 tar 的压缩与解压缩命令
查看>>
阿里负载均衡,配置中间证书问题(在starcom申请免费DV ssl)
查看>>
转:How to force a wordbreaker to be used in Sharepoint Search
查看>>
MySQL存储过程定时任务
查看>>
Python中and(逻辑与)计算法则
查看>>
POJ 3267 The Cow Lexicon(动态规划)
查看>>
设计原理+设计模式
查看>>
音视频处理
查看>>
tomcat 7服务器跨域问题解决
查看>>
前台实现ajax 需注意的地方
查看>>
Jenkins安装配置
查看>>
个人工作总结05(第二阶段)
查看>>
Java clone() 浅拷贝 深拷贝
查看>>
深入理解Java虚拟机&运行时数据区
查看>>
02-环境搭建
查看>>
spring第二冲刺阶段第七天
查看>>
搜索框键盘抬起事件2
查看>>
阿里百川SDK初始化失败 错误码是203
查看>>