2017-08-27 38 views
1

我此刻與MarkLogic POJO數據綁定接口工作時JSONMappingException。我可以爲MarkLogic編寫POJO。現在我想搜索這些POJO並檢索搜索結果。我遵循以下指示:https://docs.marklogic.com/guide/java/binding#id_89573但是,搜索結果似乎不會返回正確的對象。我得到一個JSONMappingException。代碼如下:MarkLogic POJO數據綁定接口:執行POJO搜索

HashMap<String, MatchedPropertyInfo> matchedProperties = new HashMap<String, MatchedPropertyInfo>(); 
    PropertyMatches PM = new PropertyMatches(123,"uri/prefix/location2", "uri/prefix", 1234,0,"/aKey","/aLocation",true,matchedProperties); 
    MatchedPropertyInfo MPI1 = new MatchedPropertyInfo("matched/property/uri1", "matched/property/key1", "matched/property/location1", true,"ValueMatch1", 12, 1*1.0/3, true); 
    MatchedPropertyInfo MPI2 = new MatchedPropertyInfo("matched/property/uri2", "matched/property/key2", "matched/property/location2", true,"ValueMatch2", 14, 1.0/2.0, true); 
    PM.getMatchedProperties().put("matched/property/prefix/location1", MPI1); 
    PM.getMatchedProperties().put("matched/property/prefix/location2", MPI2); 

    PojoRepository myClassRepo = client.newPojoRepository(PropertyMatches.class, Long.class); 
    myClassRepo.write(PM); 

    PojoQueryBuilder qb = myClassRepo.getQueryBuilder(); 
    PojoPage<PropertyMatches> matches = myClassRepo.search(qb.value("uri", "uri/prefix/location2"),1); 
    if (matches.hasContent()) { 
     while (matches.hasNext()) { 
      PropertyMatches aPM = matches.next(); 
      System.out.println(" " + aPM.getURI()); 
     } 
    } else { 
     System.out.println(" No matches"); 
    } 

PropertyMatches(PM)對象成功寫入MarkLogic數據庫。這個類包含一個成員:它與"uri/prefix/location2"開始private String URI。在上面的例子中,matches.hasContent()返回true。但是,我越來越對PropertyMatches aPM = matches.next();

回答

2

搜索的POJO的錯誤MarkLogic並把它讀成Java程序需要的POJO有一個空的構造。在這種情況下PropertyMatches應該有public PropertyMatches(){}和MatchedPropertyInfo應該有public MatchedPropertyInfo(){}

0

謝謝@ sjoerd999發佈您找到的答案。我想補充一些文件的引用,這個話題在這裏討論:http://docs.marklogic.com/guide/java/binding#id_54408這裏:https://docs.marklogic.com/javadoc/client/com/marklogic/client/pojo/PojoRepository.html

另外值得注意的是,你可以在consructor有多個參數,你就必須做到這一點的傑克遜的方式。這裏有兩種方法的示例(帶註釋和不帶):https://manosnikolaidis.wordpress.com/2015/08/25/jackson-without-annotations/

我建議使用註釋,因爲它是與Jackson一起構建的。但是,如果你想這樣做沒有註解,下面的代碼:

ObjectMapper mapper = new ObjectMapper(); 

    // Avoid having to annotate the Person class 
    // Requires Java 8, pass -parameters to javac 
    // and jackson-module-parameter-names as a dependency 
    mapper.registerModule(new ParameterNamesModule()); 

    // make private fields of Person visible to Jackson 
    mapper.setVisibility(FIELD, ANY); 

如果你想與PojoRepository要做到這一點,你必須使用不支持getObjectMapper方法來獲得ObjectMapper並調用registerModule和setVisibility上:

ObjectMapper objectMapper = ((PojoRepositoryImpl) myClassRepo).getObjectMapper();