简体   繁体   中英

Object deserializtion in Redisson for a class with only parameterized constructor

I've a java object(ComponentType.java) that I need to store in Redis. I'm using Redisson as client library. The object has an instance variable (ComponentType) which only has one private parameterized constructor. The ComponentType class has been generated using castor. In Redisson , the serialization part works fine but when I try to deserialize the object, I get the following exception

Exception in thread "main" org.redisson.client.RedisException: Unexpected exception while processing command
    at org.redisson.command.CommandAsyncService.convertException(CommandAsyncService.java:324)
    at org.redisson.command.CommandAsyncService.get(CommandAsyncService.java:167)
    at org.redisson.RedissonObject.get(RedissonObject.java:75)
    at org.redisson.RedissonMap.put(RedissonMap.java:256)
    at tester.RedissonIPWCTaskTester.populateMap(RedissonIPWCTaskTester.java:67)
    at tester.RedissonIPWCTaskTester.main(RedissonIPWCTaskTester.java:51)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.mae.component.valueobject.types.ComponentType]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)
 at [Source: (io.netty.buffer.ByteBufInputStream); line: 1, column: 769] (through reference chain: com.mae.component.valueobject.ComponentVO["_type"])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:256)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1134)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:298)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:168)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:135)
    at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:120)
    at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:91)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:1021)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:493)

The exception is resolved when the constructor of ComponentType is manually modified as below

@JsonCreator
private ComponentType(@JsonProperty("type") int type, @JsonProperty("value") java.lang.String value) {
    super();
    this.type = type;
    this.stringValue = value;
} 

I would appreciate help with following questions

  1. Is there a way to generate java classes using castor that supports annotations.

  2. Any other serialization/deserialization technique I could use in Redisson client to support objects having parameterized constructor only.

Summary of my blog post :

With Java 8 you can optionally include constructor metadata in compiled code and Jackson can use it instead of requiring @JsonCreator and @JsonProperty .

To achieve that:

  • compile with passing -parameters to javac
  • include and register jackson-module-parameter-names

Jackson will then be able to use the non-annotated constructor generated by castor .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM