Marklogic - 在java api中将pojo作为json文档插入

时间:2016-06-21 11:07:55

标签: marklogic marklogic-8

我想使用java api将pojo对象作为json文件插入marklogic中。我使用this示例作为参考,用于将pojo作为xml文档插入。

我无法使用JSON句柄注册我的pojo类。

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();
    }
}

如果我在这个例子中错了,请告诉我正确的方法并帮助我解决这个问题。

感谢阅读。

1 个答案:

答案 0 :(得分:3)

虽然您可以使用JAXB将您的pojo序列化为JSON,但许多人更喜欢Jackson和JacksonDatabindHandle。查看example in JacksonDatabindTest并注意the City class is registered on lines 68-69

或者,如果您不需要控制JSON在数据库中的外观,那么持久化Pojos最简单的方法就是使用POJO Data Binding Interface