This argument really boils down to whether you want to use POJOs or Maps for the data you are dealing with, as that is the fundamental difference between the 2 styles of handling data.
JsonObject (from jee7) is just a Map (the actual type signature extends Map<String, JsonValue>
).
Using POJO's (eg deserialised using Jackson) are real objects.
So if you dont use domain/model classes for your data normally then I guess a Map
will do. If you do (like most developers would), then it's a good idea to extend this practice to your API contracts.
To address specific points:
JsonObject saves from unnecessary data serialisation at the same time reducing number of classes heavily.
It doesn't, both of these JSON API's require serialisation/deserialisation, which is the process of turning wire data into an object. I would bet that Jackson is faster than JsonObject, however as Satyendra Kumar pointed out benchmarking is going to prove it one way or other.
while using pojo we are binding tightly with the rest service, while jsonObject gives freedom
If you are using the data from the API you are bound to the data model, whether you get the data from a property foo
by response.getFoo()
or response.get("foo")
.
For a really comprehensive discussion on the tradeoffs between classes and maps, check out this SoftwareEngineering post
It's worth pointing out that using Jackson/POJOs doesn't mean you are tightly coupled to your local model of a REST response. Jackson has excellent support for the Tolerant Reader pattern, which means that new fields in the API response will not break deserialisation, and a bunch of annotations like @JsonCreator
, @JsonValue
, @JsonIgnore
for mapping the API responses back to your local domain model.
It also provides powerful tools for dealing with collections and generics.
Finally if the service you are interacting with is big and complex, if they provide a swagger/raml spec then you can generate the models based on that spec which should greatly reduce the time taken to curate the POJOs.