I have a bean defined as such:
public static class TestBean { private String a; private Long b; public TestBean(String a, Long b) { this.a = a; this.b = b; } public String getA() { return a; } public Long getB() { return b; } }
It models some types of business, and I cannot create it (using JPA). Some of my APIs allow the user to get an idea of ββserializing bean as JSON using Jackson (via JAX-RS), and I would like to add a list of related links to this view.
Jackson's usual JSON serialization would be (for a = "aa" and b = 2L):
{"a":"aa","b":2}
And I would like the links to appear as
{"a":"aa","b":2, "links":[{"rel":"rel","href":"href://"},{"rel":"rel2","href":"href://2"}]}
Possible workaround
I would prefer not to add the getLinks () method to my bean, it is defined for this view.
Just using a compound object will result in serialization, for example:
{"data":{"a":"aa","b":2},"links":[{"rel":"rel","href":"href://"}]}
With whom I could live, but this is not what I was looking for in the long run.
Current solution
I would like to avoid manipulating the JSON string or reloading it in Map to insert my extra values. At the moment, the solution I came up with looks terribly confusing:
Current scary decision:
Did I miss something obvious in Jackson to achieve this? Or am I completely unfamiliar with my requirements?