2016-06-21 32 views
3

我想在使用java api的marklogic中插入pojo對象作爲json文檔。我使用this作爲插入pojo作爲xml文檔的參考。Marklogic - 在Java api中插入pojo作爲json文檔

我無法註冊我的pojo類與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(); 
    } 
} 

如果我在這個例子中錯了,請讓我知道正確的方式,並幫助我解決這個問題。

感謝您的閱讀。

回答

3

儘管您可以使用JAXB將您的pojo序列化爲JSON,但許多人更喜歡Jackson和我們的JacksonDatabindHandle。請參閱example in JacksonDatabindTest並注意the City class is registered on lines 68-69。或者,如果您不需要控制數據庫中JSON的外觀,最簡單的方法是使用POJO Data Binding Interface

+0

謝謝Sam的回覆。我會試用JacksonDataBindTest。我嘗試了POJO與接口綁定的例子。這種方法有一個問題,我們如何在保存文檔時設置文檔的uri? – RCS

+0

Sam,我嘗試了JacksonDataBind示例並能夠將pojo插入到marklogic數據庫中。在更新(添加一個屬性)的情況下需要一個建議現有文檔。有兩種方法我可以想到,要麼我會更新POJO,並將其寫回到DB或我會做部分更新,如JSON修補程序示例。考慮到我有大量數據需要處理,哪種方法更好,性能更好。 – RCS

+0

您不需要使用PojoRepository來設置uri,它是基於類名和唯一標識(標記爲@Id標註)爲您設置的。 –