2017-04-19 157 views
1

请帮助我,当将json字符串强制转换为java用户定义的对象时,出现异常。如何解决异常org.codehaus.jackson.map.exc.UnrecognizedPropertyException

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

我还发现在这里计算器许多链接,并将所有建议型号的领域@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

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

回答

0

你得到的异常,因为千斤顶的映射区分大小写。 默认情况下,cassandra使每个列名称为tolowercase。这就是为什么您的字段名称(acknowledgedBy)和cassandra cassandra的列名称(acknowledgeedby)不匹配。

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

mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);