Search code examples
marklogicmarklogic-8

Marklogic - Insert pojo as json document in java api


I want to insert pojo object as json document in marklogic using java api. I am using this example as reference which is for inserting pojo as xml document.

I am not able to register my pojo class with the handle for JSON.

public class JSONDocument {
    public static void main(String[] args) throws JAXBException, IOException {
        run(Util.loadProperties());
    }
    @JsonRootName(value = "product")
    static public class Product {
        @JsonProperty
        private String name;
        @JsonProperty
        private String industry;
        @JsonProperty
        private String description;
        public Product() {
            super();
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getIndustry() {
            return industry;
        }
        public void setIndustry(String industry) {
            this.industry = industry;
        }
        public String getDescription() {
            return description;
        }
        public void setDescription(String description) {
            this.description = description;
        }
    }

    public static void run(ExampleProperties props) throws JAXBException {

        runShortcut(props);

        System.out.println("Wrote, read, and deleted "+Product.class.getName()+" using JAXB");
    }
    public static void runShortcut(ExampleProperties props) throws JAXBException {
        // register the POJO classes like JAXB - JAXBHandle.newFactory(Product.class)

        DatabaseClientFactory.getHandleRegistry().register(
            // Need help here for - registering pojo for JSON

        );
        // create the client
        DatabaseClient client = DatabaseClientFactory.newClient(
                props.host, props.port, props.writerUser, props.writerPassword,
                props.authType);

        // create a manager for JSON documents
        JSONDocumentManager docMgr = client.newJSONDocumentManager();

        // create an instance of the POJO class
        Product product = new Product();
        product.setName("FashionForward");
        product.setIndustry("Retail");
        product.setDescription(
                "(Shortcut) Creates demand with high prices, hours from midnight to dawn, and frequent moves");

        // create an identifier for the document
        String docId = "/example/"+product.getName()+".json";

        // write the POJO as the document content
        docMgr.writeAs(docId, product);

        // ... at some other time ...

        // read the POJO from the document content
        product = docMgr.readAs(docId, Product.class);

        // log the persisted Json document
        System.out.println(docMgr.readAs(docId, String.class));


        // release the client
        client.release();
    }
}

If i am wrong in this example please let me know the right way and help me solve this.

Thanks for reading.


Solution

  • While you could possibly use JAXB to serialize your pojo to JSON, many prefer Jackson and our JacksonDatabindHandle. See an example in JacksonDatabindTest and notice that the City class is registered on lines 68-69.

    Or, if you don't need control over what your JSON looks like in the database, the easiest way to persist pojos is with the POJO Data Binding Interface.