繁体   English   中英

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

[英]How to resolve exception org.codehaus.jackson.map.exc.UnrecognizedPropertyException

请将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使每个列名称为tolowercase。 这就是为什么你的字段名称(acknowredgedBy)和cassandra cassandra的列名(acknowgedby)不匹配的原因。

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

mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM