繁体   English   中英

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

[英]Wire concrete implementation to interface in blueprint.xml

背景

希望将具体类连接到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" />

例外

杰克逊错误消息:

引起原因:com.fasterxml.jackson.databind.JsonMappingException:无法构造NotificationRequest的实例:抽象类型要么需要映射到具体类型,要么具有自定义反序列化器,或者包含其他类型信息

反序列化

可以使用以下方法反序列化该类:

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

尽管这可行,但并不能完全将实现与接口分离。

如何将具体实现连接到blueprint.xml从而不必使用@JsonDeseralize批注?

环境

  • Java 1.8
  • 阿帕奇骆驼2.x
  • JAX-RS 2.1
  • 杰克逊JAX-RS JSON Provider 2.9.x
  • OSGi 1.2.1
  • 不使用Spring

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

或使用扩展StdSerializer的自定义解串器:

@JsonDeserialize(using = YourDeserializer.java)

或使用多态和@JsonTypeInfo + @JsonSubtypes批注。

暂无
暂无

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

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