theGamblerRises theGamblerRises - 3 months ago 9
Java Question

How to get plain JSON data in jersey REST implementation?

If we have a POJO class, then we can map it with some incoming JSON. I am struggling to find out a way by which I can just have all plain json value inside.

For ex.

{
"macro_tasks": [
{
"id": "cc5cee68-c1e5-4396-987b-c68559399186",
"label": "consi-1",
"name": "Consi 1",
"project_id": "82d1e463-1bb1-42d3-9adc-9e0d5848d139",
"creator_id": null,
"created_at": null,
"updated_at": null,
"meta_data": {
"key1": "value1",
"key2": 321
}
}
]
}


Here
meta_data
is of JSON type which can keep changing its values inside. So I cannot map it with some POJO class.

public class MacroTask {

private UUID id;
private String label;
private String name;

private UUID projectId;

private UUID creatorId;

private String createdAt;

private String updatedAt;

private <some data type> meta_data;

//getter and setter


Is there any way to get plain JSON data and use it in code and dump into DB [we are using PostgreSQL, which supports
jsonb
type.]

Answer

I solved it by using Generic JSON type, Jackson's JsonNode. It has some drawbacks as I heard, but I am giving it a try. So far, it's working as per expectations. I will update if I face any challenge or I find any better solution.