Shall I use POJO or JSONObject for REST calls
I see the following advantages in having pojos
1) Readability - You will not really know the structure of a complex json. writing a simple get will require one to know the structure of the json. Please refer to the "POJOs over JSON Objects" section of my article here -> https://medium.com/tech-tablet/programming-went-wrong-oops-38d83058885
2) Offers Type Checks - We could easily assign a Cat to a Dog and not even know about it till runtime.
3) Feels more object-oriented with Composition & encapsulation - It's easy to understand the designer's perspective with a POJO. A Car which IS-A Vehicle that HAS-A Wheel.
4) You could choose what you wanna deserialize and keep only that in memory - When deserializing the object that we have just received over the network, with a JSON Object, there is no way to choose what has to be deserialized and stored into memory. If you have an object of 1 MB size where only 200 Bytes is your payload, we will end up holding the entire 1 MB object in memory if we don't use POJOs.
5) Allows collection to be used and stream operations on them in a legible way - There is no native support for stream operations in a JsonNode. We will need to use a StreamStupport object which could be avoided.
6) Allows cross framework referencing. With a few annotations, you can choose to map specific fields to a database client - When you use an ORM framework for you database, it's easy to annotate and map entities to the database schema.
7) Naturally supports design patterns
8) Minimalizing non-native dependencies - Why do you have to use a JsonNode or equivalent that does not come by itself in native Java? Especially if it has the above disadvantages.
If you are concerned about the rituals that come with a pojo like having getters, setters etc, take a look at "Lombok". This library will help you create your pojos in a concise way and still reap the above benefits.
On the other hand if you are are dealing with a hard to change legacy API that responds with a dynamically changing response, JsonNode is a quick win candidate.
It really depends on the situation and the nature of your application. If the data your are receiving has no fixed structure/field names/data types or it is ever changing then of course using JsonObject makes more sense.
On the other hand, if it is of fixed structure and involves operations which access the fields, its better to go with pojo. It has better readability IMO.
Depends on circumstances:
Use Pojo :
- If the data fields are fixed like their name, type use Pojos, as it will be clear and easily readable.
- Refactoring will be easier if it is used in many places.
- If you want to persist this data in db then modification of data will also be easy. Suppose there is a data in your pojo which is used while performing calculation, but you don't want save it in db ( so you can simply do it by using
@Transient
keyword in java, but if you have used JsonObject this would have required some code changes). - Suppose you don't want to pass null values in the response, you can easily do it using pojos by using
@JsonInclude(Include.NON_EMPTY)
in java.
Use JsonObject:
- If data fields are not fixed like their type or structure, so you won't be able to map it in pojo.