开发过程中经常会出现接口返回的字符串要转化为实体类对象,但是实体类中的字段有可能不完整 ,解析json串为实体对象的时候就会报错 com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field
异常反复出现
Exception in thread "Thread-62" java.lang.IllegalArgumentException: Unrecognized field "y_value" (class org.jeecg.modules.system.entity.KafkaGasDensity), not marked as ignorable (12 known properties: "productKey", "createdTime", "devNo", "yvalue", "deviceKey", "type", "id", "evalue", "time", "areaNo", "lvalue", "timestamp"])
at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: org.jeecg.modules.system.entity.KafkaGasDensity["y_value"])
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:4236)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:4167)
at org.jeecg.kafka.KafkaConsumerClient.run(KafkaConsumerClient.java:95)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "y_value" (class org.jeecg.modules.system.entity.KafkaGasDensity), not marked as ignorable (12 known properties: "productKey", "createdTime", "devNo", "yvalue", "deviceKey", "type", "id", "evalue", "time", "areaNo", "lvalue", "timestamp"])
at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: org.jeecg.modules.system.entity.KafkaGasDensity["y_value"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:855)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1212)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1604)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1582)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:299)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:156)
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:4231)
... 3 more
使用转译的代码
JSONObject jsonObject = JSONObject.parseObject(record.value());
Object data = jsonObject.get("data");
String deviceKey = String.valueOf(jsonObject.get("deviceKey"));
String productKey = String.valueOf(jsonObject.get("productKey"));
Long timestamp = Long.parseLong(String.valueOf(jsonObject.get("timestamp")));
String type = String.valueOf(jsonObject.get("type"));
ObjectMapper objectMapper = new ObjectMapper();
if ("power222".equals(record.topic())) {
KafkaSmartElectricity kafkaSmartElectricity = objectMapper.convertValue(data, KafkaSmartElectricity.class);
long nowSecond = DateUtil.nowSecond();
long startSecond = DateUtil.todaySecondInClock000();
kafkaSmartElectricity.setId(startSecond);
kafkaSmartElectricity.setCreatedTime(nowSecond);
kafkaSmartElectricity.setDeviceKey(deviceKey);
kafkaSmartElectricity.setProductKey(productKey);
kafkaSmartElectricity.setTimestamp(timestamp);
kafkaSmartElectricity.setType(type);
playData(getElectricitySqls(kafkaSmartElectricity));
}
解决方案
加在实体类上忽略解析字段注解 @JsonIgnoreProperties(ignoreUnknown = true)
目的:防止json字符串转成实体对象时因未识别字段报错
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "kafka_smart_weather")
@JsonIgnoreProperties(ignoreUnknown = true)
public class KafkaSmartWeather {
@TableId(value = "id", type = IdType.INPUT)
private Long id;
。。。