【问题标题】:Marklogic - Insert pojo as json document in java apiMarklogic - 在 java api 中将 pojo 作为 json 文档插入
【发布时间】:2016-06-21 11:07:54
【问题描述】:

我想使用 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();
    }
}

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

感谢阅读。

【问题讨论】:

    标签: marklogic marklogic-8


    【解决方案1】:

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

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

    【讨论】:

    • 感谢 Sam 的回复。我会尝试 JacksonDataBindTest。我尝试了使用接口绑定 POJO 的示例。这种方式有一个问题,我们如何在保存文档的同时设置文档的uri?
    • Sam,我尝试了 JacksonDataBind 示例并能够将 pojo 插入到 marklogic 数据库中。在更新(添加属性)现有文档的情况下需要一个建议。我可以想到两种方法,要么我将更新 pojo 并将其再次写回 db,要么我将像 JSON 补丁示例一样进行部分更新.考虑到我需要处理大量数据,哪种方法会更好且性能更好。
    • 你不用PojoRepository设置uri,它是根据类名和唯一id(用@Id注解标记的)为你设置的。
    • 我认为衡量性能的最佳方法是使用您的数据进行尝试。鉴于我对您的申请知之甚少,很难说。请记住,运行时性能并不是唯一的因素。
    • 我明白了山姆的意思,我们不能对性能进行评判,但我的问题是关于用更好的方法将整个 pojo 对象替换为 josn/向现有 json 添加补丁。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多