Search code examples
jsonbackbone.jsresteasyjettison

RESTEasy / Jettison, return Java object as JSON without the root node


I'm using RESTEasy to return Java objects as JSON objects (which is using Jettison Mapped Convention for JSON marshelling).

But I don't want it to return the root node.

For example

@XmlRootElement
public class Car{
    private Integer id;
    private String name;
}

An object of this class would result in JSON:

{"Car":{"id":6,"name":"someName"}}

Because it's actually coming from

<Car>
    <id>6</id>
    <name>someName</name>
</Car>

But I don't want the root node. I just want:

{"id":6,"name":"someName"}

So I can use it with client libraries likes Backbone.js

Is there any way (some annotation) to force this on the JSON marshelling ?

Sam,


Solution

  • I was faced with the exact same problem. After doing some research I found people suggested using resteasy-jackson-provider instead of jettison. It was claimed that jettison has a few issues and that what you're experiencing is one of them. I switched to Jackson and found that it solved this issue and probably a few others that I wasn't aware of. If you're using maven:

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson-provider</artifactId>
        <version>2.1.0.GA</version>
    </dependency>
    

    If you do this, you may see some collisions between jettison. To avoid those make sure you don't have the jettison jars on your classpath.