使用Spring-Data-Rest以XML而不是JSON的形式返回响应 [英] Use Spring-Data-Rest to return response as XML instead of JSON

查看:146
本文介绍了使用Spring-Data-Rest以XML而不是JSON的形式返回响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring-data-rest 当前为RC1(标题为GA 7月16日),但是文档仍然有些粗略.到目前为止,我发现的所有示例代码均显示默认为JSON的响应,但我需要XML,理想情况下需要基于ACCEPT标头的XML或JSON.我在 DZone链接中的一些注释中找到了一个消息源,表明将支持XML. .但这是在RC1之前的M2版本中发布的.我在该项目下的问题"中也没有看到任何内容.

Spring-data-rest is currently RC1 (heading for GA July 16), but the documentation is still a little sketchy. So far all the example code I find shows responses defaulting to JSON, but I need XML, and ideally either XML or JSON based on ACCEPT header. I found one source in some comments in a DZone link that indicate XML is going to be supported. But that was posted during the M2 release, before RC1. I don't see anything in the Issues under the project either.

所以有人知道如何使RC1(或SNAPSHOT)产生XML来代替JSON或除了JSON之外.

So does anybody know how to make either RC1 (or SNAPSHOT) produce XML instead of or in addition to JSON.

推荐答案

我希望在Spring Data REST中有一个简单的答案来生成XML,但是我还没有找到答案.我们默认使用JSON,因为我们认为这是用于传输对象的超级简单且轻量的协议",并且在大多数情况下都可以正常工作.

I wish there was an easy answer to producing XML in Spring Data REST, but I haven't found one yet. We defaulted to using JSON because we figured that's a super easy and lightweight "protocol" for transmitting objects and will work good enough for most cases.

XML存在一些问题,我们还没有找到合理的答案:

There are a couple of problems with XML that we haven't found reasonable answers for:

  • 如何用XML表示对象?我应该使用属性名称作为元素名称还是使用标准元素名称并将属性名称放入属性中?
  • 我在哪里可以识别属性的类型(无论是复杂的还是简单的,Long,BigInteger或您拥有什么)?
  • 我是否不需要所有自定义映射信息,而只使用JAXB或Spring OXM?
  • 我如何处理链接?我是否使用Atom命名空间链接元素?
  • 如果我已经在使用Atom,那么为什么不对所有内容都使用Atom表示呢?

由于自从这个项目如此新以来,回答这些问题必然会比我们迄今为止需要更多的社区投入,所以我想我们要等到1.0版之后的版本才能添加XML支持.如果在这个方向上有足够的动力,优先级显然可能会改变,但是我只是看不到下周会发生.

Since answering these questions will necessarily involve more community input than we've had yet since the project is so new, I was thinking we'd wait until a post-1.0 release to add XML support. Priorities could obviously change if there's enough momentum in that direction, but I just don't see it happening in the next week.

您可以重写某些机器(毕竟,这只是一个Spring MVC应用程序),以产生您想要的任何类型的响应.如果您使用最新的快照并覆盖 RepositoryRestMvcConfiguration.contentNegotiatingViewResolver()方法,则可以提供完全不同的DTO表示形式走出出口商.这在Wiki中称为 .您必须检查 JsonView 的源代码,以获得有关DTO内部外观的必要详细信息. ,但是用MyOwnXmlView替换JsonView相对容易.

There is some machinery you can override (this is just a Spring MVC application, after all) to produce any kind of a response you want. If you use the latest snapshot and override the RepositoryRestMvcConfiguration.contentNegotiatingViewResolver() method, you can provide an entirely different representation of the DTO coming out of the exporter. This is referred to in the wiki. You'll have to check the source code of JsonView to get the necessary details on what the DTO looks like internally, but it would be relatively easy to replace JsonView with MyOwnXmlView.

注意:这仅适用于当前版本的REST导出器. GA版本将使用不同的机制来呈现输出.不过,我将提供钩子来执行您想要执行的任何类型的输出渲染,因此您应该能够通过在配置上设置属性来覆盖输出渲染.如果为此版本创建View子类,则可能只是将其更改为GA版本的HttpMessageConverter的问题.

NOTE: This will only work for the current version of the REST exporter. The GA version will have different machinery to render output. I'll be providing hooks for doing whatever types of output rendering one wants to do, though, so you should be able to override output rendering by setting a property on the configuration. If you create a View subclass for this version, it will likely only be a matter of changing it to an HttpMessageConverter for the GA version.

这篇关于使用Spring-Data-Rest以XML而不是JSON的形式返回响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆