简体   繁体   English

如何使用Playframework呈现特殊的XML / JSON Flavors

[英]How to render special XML/JSON Flavours with Playframework

According to James Wards Play Tutorial it's very easy to get a JSON out of your model. 根据James Wards Play Tutorial ,从模型中获取JSON非常容易。 Also with XML this should be quite simple. 使用XML也应该非常简单。

But most of the time, I have the requirement to build not just an plain XML or JSON Endpoint, but furthermore to deliver special flavours of those. 但大多数时候,我不仅需要构建纯XML或JSON端点,还要提供特殊的那些。 In my case this is GeoJSON or TopoJSON . 在我的例子中,这是GeoJSONTopoJSON But also in XML, it could be a simple RSS or ATOM Feed you have to deliver out of your model. 但是在XML中,它可能是一个简单的RSS或ATOM Feed,你必须从模型中提供。 Also building a XML fitting to a very nasty XSD schema is still a case sometimes. 同时,构建适合非常讨厌的XSD架构的XML也是一种情况。

What options do you have in play to perform this, or which one of the following would you recommend?: 您有什么选择来执行此操作,或者您建议使用以下哪一项?:

  1. In case of GeoJSON/TopoJSON: Activate JSON as a template format, and create JSON Templates 对于GeoJSON / TopoJSON:激活JSON作为模板格式,并创建JSON模板
  2. In case of ATOM/RSS: Just use an XML Template 对于ATOM / RSS:只需使用XML模板
  3. Some way to modify the JSON response coming from toJson(tasks) ? 一些修改来自toJson(tasks)的JSON响应的方法?
  4. Use of a fancy library which does all that out of the box, and everyone knows about it, except me? 使用一个开箱即用的精美图书馆,每个人都知道它,除了我?
  1. If you're doing GeoJSON, just annotate your objects with Jackson annotations according to the GeoJSON spec, it's not hard. 如果您正在使用GeoJSON,只需根据GeoJSON规范使用Jackson注释注释您的对象,这并不难。 If it is hard, then there are a few libraries out there that come with Java objects with the necessary annotations already for you, eg: https://github.com/opendatalab-de/geojson-jackson 如果它很难,那么有一些库已经带有Java对象,带有必要的注释,例如: https//github.com/opendatalab-de/geojson-jackson
  2. An XML template is probably the simplest from Java. XML模板可能是Java中最简单的。
  3. What's your use case? 你的用例是什么? toJson returns a Jackson JSONNode. toJson返回Jackson JSONNode。 You can modify it as much as you want. 您可以根据需要进行修改。 But the better thing to do would be to use Jackson annotations on your objects to get the format right in the first place. 但最好的办法是在你的对象上使用Jackson注释来获得正确的格式。
  4. I think you're referring to Jackson, it can do everything you want. 我想你是指杰克逊,它可以做你想做的一切。 It can even do XML if you want it to. 如果你想要它甚至可以做XML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM