MarkLogic POJO 数据绑定接口:执行 POJO 搜索时出现 JSONMappingException
MarkLogic POJO Data Binding Interface: JSONMappingException when performing a POJO search
我目前正在使用 MarkLogic POJO 数据绑定接口。我能够将 POJO 写入 MarkLogic。现在我想搜索那些 POJO 并检索搜索结果。我正在按照以下说明进行操作:https://docs.marklogic.com/guide/java/binding#id_89573 但是,搜索结果似乎 return 不是正确的对象。我收到 JSONMappingException。这是代码:
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 数据库。此 class 包含一个成员:private String URI
,它由 "uri/prefix/location2"
发起。上例中的 matches.hasContent()
return 为真。但是,我在 PropertyMatches aPM = matches.next();
上遇到错误
在 MarkLogic 中搜索 POJO 并将它们读入您的 Java 程序需要 POJO 具有空构造函数。在这种情况下 PropertyMatches
应该有 public PropertyMatches(){}
而 MatchedPropertyInfo 应该有 public MatchedPropertyInfo(){}
感谢@sjoerd999 发布您找到的答案。只是为了添加一些文档参考,这里讨论了这个主题:http://docs.marklogic.com/guide/java/binding#id_54408 and here: https://docs.marklogic.com/javadoc/client/com/marklogic/client/pojo/PojoRepository.html.
同样值得注意的是你可以在构造器中有多个参数,你只需要按照 Jackson 的方式来做。以下是两种方式的示例(带注释和不带注释):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();
我目前正在使用 MarkLogic POJO 数据绑定接口。我能够将 POJO 写入 MarkLogic。现在我想搜索那些 POJO 并检索搜索结果。我正在按照以下说明进行操作:https://docs.marklogic.com/guide/java/binding#id_89573 但是,搜索结果似乎 return 不是正确的对象。我收到 JSONMappingException。这是代码:
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 数据库。此 class 包含一个成员:private String URI
,它由 "uri/prefix/location2"
发起。上例中的 matches.hasContent()
return 为真。但是,我在 PropertyMatches aPM = matches.next();
在 MarkLogic 中搜索 POJO 并将它们读入您的 Java 程序需要 POJO 具有空构造函数。在这种情况下 PropertyMatches
应该有 public PropertyMatches(){}
而 MatchedPropertyInfo 应该有 public MatchedPropertyInfo(){}
感谢@sjoerd999 发布您找到的答案。只是为了添加一些文档参考,这里讨论了这个主题:http://docs.marklogic.com/guide/java/binding#id_54408 and here: https://docs.marklogic.com/javadoc/client/com/marklogic/client/pojo/PojoRepository.html.
同样值得注意的是你可以在构造器中有多个参数,你只需要按照 Jackson 的方式来做。以下是两种方式的示例(带注释和不带注释):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();