1

I am building a REST API with a payload that has a property called jsonContent, which holds any valid json.

{
  "name":"foo",
  "jsonContent":{ "abc":"My content"}
}

On the server side I want to map the it to a generic java object ,and eventually save the whole object to mongodb

private String name;
private ?????? jsonContent

I am using jackson for mapping json to java. How do I declare my java object so any json content can be used.

1
  • 1
    Any json can be represented in terms of maps (String to Object) and lists. I realise it won't work with Jackson, but you could parse the json into a series of maps and lists and then serialize those. Commented Aug 25, 2016 at 22:52

2 Answers 2

1

Use JsonNode:

private JsonNode jsonContent;
Sign up to request clarification or add additional context in comments.

1 Comment

It works but when the object is saved to mongodb it is deserialized as follows with extra properties and nesting ` {"_class" : "com.fasterxml.jackson.databind.node.ObjectNode", "_children" : { "title" : { "_value" : "Mytitle", "_class" : "com.fasterxml.jackson.databind.node.TextNode" } }, "_nodeFactory" : { "_cfgBigDecimalExact" : false }`
1

I answer my own question, Following worked just fine for me

private Map<String,Object> jsonContent;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.