简体   繁体   中英

Why doesn't DefaultJackson2JavaTypeMapper.toJavaType() support abstract classes and interfaces as inferred types?

I would like to send messages in JSON format through RabbitMQ from one Java application to another using spring-amqp (1.7.4). The two applications do not share the same domain model classes.

I have a single generic @RabbitListener annotated method on the receiving end, that takes a single argument of type Event , an interface.

I have properly configured Jackson to handle the Event type hierarchy on both sides, yet, spring-rabbit won't convert my JSON message into the proper type because DefaultJackson2JavaTypeMapper does not support inferred abstract classes or interfaces.

If I define a custom JavaTypeMapper that extends DefaultJackson2JavaTypeMapper and does the following, it works perfectly fine:

@Override
public JavaType toJavaType(MessageProperties properties) {
    boolean hasInferredTypeHeader = hasInferredTypeHeader(properties);

    if (hasInferredTypeHeader && getTypePrecedence().equals(TypePrecedence.INFERRED)) {
        // do not check for abstract classes and interfaces here
        JavaType targetType = fromInferredTypeHeader(properties);

        return targetType;
    }

    return super.toJavaType(properties);
}

Wouldn't it be better to leave the user in charge of how the conversion is to take place (either using spring-rabbit conventions or using Jackson directly)? Maybe add a flag that enables abstract classes and interfaces support? Is there something I'm missing?

Feel free to open an Improvement JIRA Issue .

Contributions are welcome along with suitable test cases.

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