jackson cannot deserialize (spring cloud stream kafka)

≯℡__Kan透↙ 提交于 2020-01-23 09:14:07

问题


I am trying to read a json message from kafka and got an exception, which says Jackson cannot deserialize the json to POJO.

The json is like {"code":"500","count":22,"from":1528343820000,"to":1528343880000}, which is an output of kafka stream.

The POJO declares all attributes of the json, and is exactly the same POJO to produce the json message. So I have no idea why it would happen.

I am using spring cloud stream 2.0.0.RELEASE.

Any help would be appreciated. Thanks.

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

Stacktrace:

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

回答1:


Jackson needs access to the default constructor to deserialize, add the default constructor to the pojo, ie:

public CodeCount() {
}



回答2:


You can annotate the existing constructor, and args, and Jackson will use this:

@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;
}

Passing in the dates may complicate it a bit, but it is definitely still possible.



来源:https://stackoverflow.com/questions/50735613/jackson-cannot-deserialize-spring-cloud-stream-kafka

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!