将具体实现连接到 blueprint.xml 中的接口

Wire concrete implementation to interface in blueprint.xml

背景

希望将具体 class 连接到 blueprint.xml.

中 REST 服务器的 @POST 方法的参数

问题

Jackson 数据绑定找不到方法的 send 参数的具体实现。

代码

Java

REST 服务器只有一个 send 方法,定义为:

public interface NotificationServer {
    @POST
    Response send(NotificationRequest notificationRequest);
}

NotificationRequest也是一个接口:

public interface NotificationRequest {

NotificationServer 的具体实现,称为 EmailNotificationServerImpl,实现接口:

public class EmailNotificationServerImpl implements NotificationServer {
    @Override
    public Response send(final NotificationRequest request) {

蓝图

blueprint.xml的相关部分包括:

<bean class="EmailNotificationServerImpl" id="emailNotificationServerImpl">...</bean>
<bean class="NotificationRequestImpl" id="notificationRequestImpl" />
<service interface="NotificationRequest" ref="notificationRequestImpl" />

异常

Jackson 错误信息:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of NotificationRequest: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information

反序列化

可以反序列化 class 使用:

@JsonDeserialize(as = NotificationRequestImpl.class)
public interface NotificationRequest {

虽然这可行,但它并没有将实现与接口完全分离。

问题

如何在blueprint.xml中连线具体实现,从而不需要@JsonDeseralize注解?

环境

<jaxrs:providers> 标记内的 JAX-RS 服务器定义中使用扩展 JacksonJsonProvider 的自定义提供程序。

或者使用扩展 StdSerializer:

的自定义反序列化器
@JsonDeserialize(using = YourDeserializer.java)

或者使用多态和@JsonTypeInfo + @JsonSubtypes注解