Currently I am working in a project where we are using restful services and jdbc connection. We can handle request response either by jsonObject or with a POJO class. I am confused between these. which one to use and why?
-
2see https://stackoverflow.com/questions/14172621/whats-the-advantage-of-pojo – Alien Dec 10 '18 at 06:46
-
It all depends on what you want to do and how. Using a POJO in your code is usually simpler. – Mark Rotteveel Dec 10 '18 at 11:02
3 Answers
Yes you can read response by using either but if you use jsonObject it will give you an immutable JSON object value. Now it all comes down to your requirement if you have a requirement where you don't need to change any value then you can directly read the response and send it to where ever you want but if you are doing any modification then you need POJO with getter and setter methods. Similarly in case of creating a request you can use below code:
JsonObject object = Json.createObjectBuilder().build();
but then you end up creating all the nodes and child which is ok for small request but it there are more fields in request then using POJO is a good idea.

- 499
- 4
- 15
POJO: Its a simple blue print that defines the properties with getter and setter.
jsonObject: This is a simple data interchangeable format used for client-server interaction

- 109
- 1
- 10
You can use both. If you are receiving Json request or response as string you can convert it to a Json object. Then you can map that fields into POJO class and use them through the POJO class to build your code logic.

- 135
- 2
- 11