杰克逊无法反序列化(春云流卡夫卡)

时间:2018-06-07 07:45:22

标签: jackson spring-cloud-stream

我正在尝试从kafka读取一条json消息并得到一个例外,该消息称Jackson不能将json反序列化为POJO。

json就像{"code":"500","count":22,"from":1528343820000,"to":1528343880000},它是kafka流的输出。

POJO声明了json的所有属性,并且与生成json消息的POJO完全相同。所以我不知道为什么会这样。

我正在使用spring cloud stream 2.0.0.RELEASE。

任何帮助将不胜感激。感谢。

POJO:

public class CodeCount {
    private String code;
    private long count;
    private Date from;
    private Date to;

    @Override
    public String toString() {
        return "CodeCount [code=" + code + ", count=" + count + ", from=" + from + ", to=" + to + "]";
    }

    public CodeCount(String code, long count, Date from, Date to) {
        super();
        this.code = code;
        this.count = count;
        this.from = from;
        this.to = to;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }

    public Date getFrom() {
        return from;
    }

    public void setFrom(Date from) {
        this.from = from;
    }

    public Date getTo() {
        return to;
    }

    public void setTo(Date to) {
        this.to = to;
    }
}

堆栈跟踪:

2018-06-07 15:18:51.572 ERROR 1 --- [container-0-C-1] o.s.integration.handler.LoggingHandler   : org.springframework.messaging.converter.MessageConversionException: Could not read JSON: Cannot construct instance of `com.example.CodeCount` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (byte[])"{"code":"500","count":22,"from":1528343820000,"to":1528343880000}"; line: 1, column: 2]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.example.CodeCount` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67) ~[jackson-databind-2.9.3.jar!/:2.9.3]
at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1451) ~[jackson-databind-2.9.3.jar!/:2.9.3]
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1027) ~[jackson-databind-2.9.3.jar!/:2.9.3]
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1275) ~[jackson-databind-2.9.3.jar!/:2.9.3]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:325) ~[jackson-databind-2.9.3.jar!/:2.9.3]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159) ~[jackson-databind-2.9.3.jar!/:2.9.3]
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001) ~[jackson-databind-2.9.3.jar!/:2.9.3]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3109) ~[jackson-databind-2.9.3.jar!/:2.9.3]
at org.springframework.messaging.converter.MappingJackson2MessageConverter.convertFromInternal(MappingJackson2MessageConverter.java:221) ~[spring-messaging-5.0.6.RELEASE.jar!/:5.0.6.RELEASE]
... 37 common frames omitted

2 个答案:

答案 0 :(得分:7)

Jackson需要访问默认构造函数来反序列化,将默认构造函数添加到pojo中,即:

public CodeCount() {
}

答案 1 :(得分:0)

您可以注释现有的构造函数和args,Jackson将使用以下代码:

@JsonCreator
public CodeCount(@JsonProperty("code") String code, 
                 @JsonProperty("count") long count, 
                 @JsonProperty("from") Date from, 
                 @JsonProperty("to") Date to) {
    super();
    this.code = code;
    this.count = count;
    this.from = from;
    this.to = to;
}

传递日期可能会使它复杂一些,但绝对有可能。