2

I have a domain object which is being created fine from a POST method body by jackson.

@Entity("payloads")
@Data
@NoArgsConstructor
class Payload{
    @Id
    String payloadId;

    JsonNode data;
}

But when I try to persist this in mongodb I am getting the following error -

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.fasterxml.jackson.databind.node.JsonNodeFactory.

I understand that instead of using JsonNode, I could create a class for data and either embed or reference it in payloads collection. But my problem is the structure of data is dynamic and I just want to save the Json data in mongo.

Is there anyway I could achieve this?

7
  • 1
    If you have the json you can use directly the driver to save it. Maybe is a simpler solution than use morphia: Document d = new Document("_id", payloadId).append("data", Document.parse(jsonString)); dbCollection.insertOne(d); Commented May 3, 2018 at 11:23
  • The collection is Payloads. I have the json for one of the fields of the collection. Can I embed a document into another document using java driver. Commented May 3, 2018 at 11:28
  • You mean to have something like?: {_id:xxx, data:{json}}. This is what you'll get with the example above. Or you mean something flat like {_id:xxx, fields in the json here...} ? Commented May 3, 2018 at 11:36
  • It is what you mentioned first. But my actual payload document would contain other fields in addition to id. {_id:xxx, field1: {..}, field2: {..}, data:{json}} Commented May 3, 2018 at 11:39
  • 1
    Yes, you would be able to add as many fields as you want: new Document("_id", payloadId).append("data", Document.parse(jsonString)).append("field1":X).append("field2":Y); Commented May 3, 2018 at 11:41

0

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.