Lionel Koh
Lionel Koh

Reputation: 199

jersey and additional data for json reply without modifying the entity class

Let's say I have a person class with only 2 attributes, name and age. My json reply using jersey will give me something like that.

{
    "name":"john",
    "age":"21"
}

Is there a way to add in additional information into the json reply without modifying the entity itself. Let's say if I want the reply to be:

{
    "name":"john",
    "age":"21"
    "favcolor":"red"
}

Let's say favcolor is retrieved from a db or something and is not a attribute of the person class. Is it possible to do something like this?

Upvotes: 0

Views: 61

Answers (1)

user1907906
user1907906

Reputation:

From the REST client point of view, it doesn't matter how a Resource Represenation is build. If you use JAX-RS, Jersey, and JAXB, the recommended way is to make the class annotated with @XmlRootElement match the returned Resource Represenation.

I your example this would be something like this:

@XmlRootElement
class Person {
  private String name;
  private String age;
  private String favcolor;

  // Constuctor, Getter, Setter
}

This class that is serialized to JSON is not necessarily the same class that is retrieved from some backend.

Answer: There is no way I know of and I don't recommend to do what you ask.

Upvotes: 1

Related Questions