gpt4 book ai didi

java - 如何解决异常 org.codehaus.jackson.map.exc.UnrecognizedPropertyException

转载 作者:太空宇宙 更新时间:2023-11-04 11:33:58 26 4
gpt4 key购买 nike

请帮助我,当我将 json 字符串转换为 java 用户定义的对象时,出现以下异常。

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "acknowledgedby" (Class com.xchange.model.XchangeOutboundMessage), not marked as ignorable
at [Source: java.io.StringReader@3452296e; line: 1, column: 34] (through reference chain: com.xchange.model.XchangeOutboundMessage["acknowledgedby"])

我还在 stackoverflow 上找到了许多链接,并且都建议在模型字段上使用 @JsonIgnore 注释,但我不能忽略这一点。

public List getOutBoundMessageList(){
List list=new ArrayList();
ObjectMapper mapper = new ObjectMapper();
XchangeOutboundMessage xchangeOutboundMessage=null;
String json1=null;
try {

cluster = Cluster.builder().addContactPoint(contactPoints).build();

session = cluster.connect(keySpaceName);

cassandraOps = new CassandraTemplate(session);
String queryString="Select JSON * from XchangeOutboundMessage";
ResultSet result = session.execute(queryString);
int i=0;
String json1=null;
for(Row row:result) {
json1 = row.getString(i);
xchangeOutboundMessage = mapper.readValue(json1, XchangeOutboundMessage.class);
list.add(xchangeOutboundMessage);
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}

发生异常的模型类字段和getter、setter

private String acknowledgedBy;
public String getAcknowledgedBy() {
return acknowledgedBy;
}
public void setAcknowledgedBy(String acknowledgedBy) {
this.acknowledgedBy = acknowledgedBy;
}

最佳答案

由于 Jacks 映射器区分大小写,因此您收到异常。默认情况下,cassandra 使每个列名都小写。这就是为什么您的字段名称 (acknowledgedBy) 和 cassandra cassandra 的列名称 (acknowledgedBy) 不匹配。

您可以通过configure方法设置jackson映射器匹配键不区分大小写。

mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

关于java - 如何解决异常 org.codehaus.jackson.map.exc.UnrecognizedPropertyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43486771/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com