Question

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.

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

Was it helpful?

Solution

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.

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

  • How do I represent an object in XML? Do I use the property name as the element name or do I use a standard element name and put the property name in an attribute?
  • Where do I identify the type of the property (whether it's complex or simple, a Long, a BigInteger or what have you)?
  • Do I dispense with all the custom mapping information and just use JAXB or Spring OXM?
  • What do I do about links? Do I use the Atom namespace link element?
  • If I'm using Atom already, then why not use an Atom representation for everything?

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.

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.

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.

OTHER TIPS

Well with latest Spring IO Platform we can achieve this and with IO Platform we dont need to worry about version also.

Have posted how to achieve in another stack overflow link as below :

Spring Boot REST with XML Support

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top