2016-06-21 2 views
3

Java API를 사용하여 marklogic에서 json 문서로 pojo 객체를 삽입하려고합니다. pojo를 XML 문서로 삽입하기위한 참조로 this 예제를 사용하고 있습니다.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이 어떻게 보이는지 제어 할 필요가없는 경우 pojos를 지속하는 가장 쉬운 방법은 POJO Data Binding Interface입니다.

+0

Sam에게 reply를 보내 주셔서 감사합니다. JacksonDataBindTest를 시도해 보겠습니다. 인터페이스로 POJO 바인딩 예제를 시도했다. 이 접근 방식에는 한 가지 문제가 있습니다. 문서를 저장하는 동안 문서의 URI를 어떻게 설정합니까? – RCS

+0

Sam, JacksonDataBind 예제를 사용하여 pojo를 marklogic 데이터베이스에 삽입 할 수있었습니다. 기존 문서를 업데이트 (속성 추가) 할 때 하나의 제안이 필요합니다. 두 가지 방법이 있습니다. pojo를 업데이트하고 db에 다시 쓰거나 JSON 패치 예제와 같은 부분 업데이트를 할 것입니다. . 어떤 접근 방식이 더 좋고 성능이 좋은지, 나는 많은 데이터를 가지고 있다고 생각합니다. – RCS

+0

PojoRepository로 URI를 설정하지 않으면 클래스 이름과 고유 ID (@Id 주석으로 표시)에 따라 URI가 설정됩니다. –

관련 문제