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

时间:2017-04-19 05:00:44

标签: java json apache cassandra

请将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;
    }

1 个答案:

答案 0 :(得分:0)

由于Jacks mapper区分大小写,您将获得Exception。 默认情况下,cassandra使每个列名称为tolowercase。这就是为什么您的字段名称(acknowredgedBy)和cassandra cassandra的列名(已确认)不匹配。

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

mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);