简体   繁体   English

为什么DefaultJackson2JavaTypeMapper.toJavaType()不支持抽象类和接口作为推断类型?

[英]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). 我想使用spring-amqp(1.7.4)通过RabbitMQ将JSON格式的消息从一个Java应用程序发送到另一个Java应用程序。 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. 我在接收端有一个通用的@RabbitListener批注方法,该方法采用Event类型的单个参数(接口)。

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. 我已经正确配置了Jackson来处理双方的事件类型层次结构,但是,由于DefaultJackson2JavaTypeMapper不支持推断的抽象类或接口,spring-rabbit不会将我的JSON消息转换为正确的类型。

If I define a custom JavaTypeMapper that extends DefaultJackson2JavaTypeMapper and does the following, it works perfectly fine: 如果我定义了一个扩展DefaultJackson2JavaTypeMapper的自定义JavaTypeMapper并执行以下操作,则可以很好地工作:

@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)? 让用户负责转换的方式(使用spring-rabbit约定或直接使用Jackson)是否更好? 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 . 随时打开“ 改善JIRA问题”

Contributions are welcome along with suitable test cases. 欢迎提供贡献以及合适的测试用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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