【问题标题】:How to resolve exception org.codehaus.jackson.map.exc.UnrecognizedPropertyException如何解决异常 org.codehaus.jackson.map.exc.UnrecognizedPropertyException
【发布时间】:2017-09-15 03:42:20
【问题描述】:

请帮助我在将 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;
    }

【问题讨论】:

  • 假设您共享整个 Model 类以清楚地了解问题。

标签: java json cassandra


【解决方案1】:

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

你可以通过configure方法设置jackson mapper匹配key不区分大小写。

mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

【讨论】:

    猜你喜欢
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 2011-10-20
    相关资源
    最近更新 更多