Pergunta

According to James Wards Play Tutorial it's very easy to get a JSON out of your model. Also with XML this should be quite simple.

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. In my case this is GeoJSON or TopoJSON. But also in XML, it could be a simple RSS or ATOM Feed you have to deliver out of your model. Also building a XML fitting to a very nasty XSD schema is still a case sometimes.

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
  2. In case of ATOM/RSS: Just use an XML Template
  3. Some way to modify the JSON response coming from toJson(tasks)?
  4. Use of a fancy library which does all that out of the box, and everyone knows about it, except me?
Foi útil?

Solução

  1. If you're doing GeoJSON, just annotate your objects with Jackson annotations according to the GeoJSON spec, it's not hard. 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
  2. An XML template is probably the simplest from Java.
  3. What's your use case? toJson returns a 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.
  4. I think you're referring to Jackson, it can do everything you want. It can even do XML if you want it to.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top