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

Marklogic-在JavaAPI中将pojo作为json文档插入,marklogic,marklogic-8,Marklogic,Marklogic 8,我想使用JavaAPI将pojo对象作为json文档插入marklogic中。我使用示例作为参考,用于将pojo作为xml文档插入 我无法使用JSON句柄注册我的pojo类 public class JSONDocument { public static void main(String[] args) throws JAXBException, IOException { run(Util.loadProperties()); } @JsonRootN

我想使用JavaAPI将pojo对象作为json文档插入marklogic中。我使用示例作为参考,用于将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();
    }
}
如果我在这个例子中错了,请让我知道正确的方法,并帮助我解决这个问题


感谢阅读。

虽然您可以使用JAXB将pojo序列化为JSON,但许多人更喜欢Jackson和我们的JacksonDatabindHandle。请看一篇文章并注意


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

感谢Sam的回复。我将尝试JacksonDataBindTest。我尝试了使用接口绑定POJO的示例。这种方法有一个问题,我们如何在保存文档时设置文档的uri?Sam,我尝试了JacksonDataBind示例,并能够将pojo插入marklogic数据库。在更新(添加属性)现有文档时需要一个建议。我可以想到两种方法,要么更新pojo并将其重新写入db,要么像JSON补丁示例那样进行部分更新。考虑到我有大量的数据要处理,哪种方法更好、性能更好。您不必使用PojoRepository设置uri,它是根据类名和唯一id(用@id注释标记)为您设置的。我认为衡量性能的最佳方法是使用您的数据进行测试。鉴于我对你的申请知之甚少,很难说。请记住,运行时性能不是唯一的因素。我明白你的意思,Sam,我们不能对性能进行评判,但我的问题是,在将整个pojo对象替换为josn/将补丁添加到现有json中时,要采用更好的方法。