7

I currently trying out Redisson as a Redis client and so far I've been able to replace a good chunk of code with no issues. The only problem I'm having now is trying to use the distributed collections, such as Queue or List.

List<MyEntry> entries = // read some sample data from a file
RedissonClient client = // create client
RBlockingQueue<MyEntry> queue = client.getBlockingQueue("test-queue", new JsonJacksonCodec());

queue.addAll(entries);
List<MyEntry> readBack = new ArrayList<>();
queue.drainTo(readBack);

When I get to the last line, I always get this exception -

com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class java.lang.Object]: missing type id property '@class' at [Source: (io.netty.buffer.ByteBufInputStream); line: 1, column: 1439]

When I add @JsonTypeInfo to my class, it appears to work, however, most of the classes I don't have access to add the @JsonTypeInfo annotation to.

I am missing something here? One way to work around this could be to use the ByteArrayCodec and serialize/deserialize using my own ObjectMapper (edit: trying this throws another type of exception!), but if possible I'd prefer to let Redisson handle this since it offers many codecs already.

Any help is much appreciated as usual!

A bit more info - I ended up writing my own simple Codec, that simply takes a Class as a parameter, and creates a Decoder and Encoder works similiar to how the JsonJacksonCodec works, with one difference -

 private static class MyCodec<T> implements Codec {

    private final Decoder<Object> decoder = new Decoder<Object>() {
        @Override
        public T decode(ByteBuf buf, State state) throws IOException {
            return mapper.readValue((InputStream) new ByteBufInputStream(buf), type);
        }
    };

    private final ObjectMapper mapper = new ObjectMapper(new MessagePackFactory());
    private final Class<T> type;

    public MyCodec(Class<T> type) {
        this.type = type;
    }

    // rest of methods...
}

And I was able to get my example working - but this feels like a workaround, rather than a solution to the original problem, and I don't want to have to write additional Codecs for each implementation I have :)

KingTravisG
  • 1,316
  • 4
  • 21
  • 43
  • Can you show us your `MyEntry` class if that's possible? – Redisson_RuiGu Sep 05 '17 at 11:02
  • @Redisson_RuiGu it's a work project, but it's a typical POJO - every property with a JsonProperty annotation, single all argument constructor with JsonCreator. I've also had some issues were it will serialize a type as something like class: Collections.EmptyMap, but I think that is more a Jackson issue – KingTravisG Sep 05 '17 at 11:35
  • I have the same problem. I have two projects one project read and another write on redis. When i tried read the list of my Object i received the same error. How you solve this problem? – Lucas Araújo Jan 16 '19 at 18:56
  • @LucasAraújo hey, I got around this by creating the class in the bottom example - MyCodec<> - and using it anywhere I need to supply a codec! – KingTravisG Jan 16 '19 at 22:13
  • @KingTravisG I tried implementing a codec too, but my codec not worked. Could you share the MyCodec class code? – Lucas Araújo Jan 18 '19 at 20:58
  • 1
    @LucasAraújo Hey, I don't have the code as it was for a work project, but the example at the bottom is most of it, the rest is just the methods that need to be implemented! This is probably the best example of how to implement the remaining methods - https://github.com/redisson/redisson/blob/master/redisson/src/main/java/org/redisson/codec/JsonJacksonCodec.java – KingTravisG Jan 19 '19 at 12:13

2 Answers2

3

Redisson provides a default Jackson codec for classes that are NOT annotated with Jackson annotations. Your existing annotations is taking precedence over the default codec setting, hence the problem. You can try other types of codec like fst codec or supply your own compatible object mappper to the Jackson codec.

Redisson_RuiGu
  • 1,012
  • 10
  • 13
1

See Fundamental API design flaw -- with respect to encoding and serialization

I would very much like to be wrong. Currently looking for a way to address this that doesn't require major surgery.

DALDEI
  • 3,722
  • 13
  • 9