简体   繁体   中英

Why we need POJO when we can use jsonObject in Restful services

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?

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM