简体   繁体   English

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

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

Background 背景

Looking to wire a concrete class to a parameter for a REST server's @POST method within blueprint.xml . 希望将具体类连接到blueprint.xml REST服务器的@POST方法的参数。

Problem 问题

Jackson data binding cannot find the concrete implementation for the method's send parameter. Jackson数据绑定找不到方法的send参数的具体实现。

Code

Java Java的

The REST server has a single send method, defined as: REST服务器具有一个send方法,定义为:

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

The NotificationRequest is also an interface: NotificationRequest也是一个接口:

public interface NotificationRequest {

A concrete implementation of NotificationServer , called EmailNotificationServerImpl , implements the interface: NotificationServer一个具体实现称为EmailNotificationServerImpl ,实现了以下接口:

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

Blueprint 蓝图

The relevant parts of blueprint.xml include: blueprint.xml的相关部分包括:

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

Exception 例外

The Jackson error message: 杰克逊错误消息:

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

Deserialize 反序列化

It is possible to deserialize the class using: 可以使用以下方法反序列化该类:

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

While this works, it doesn't fully decouple the implementation from the interface. 尽管这可行,但并不能完全将实现与接口分离。

Question

How can the concrete implementation be wired in blueprint.xml so that the @JsonDeseralize annotation is not necessary? 如何将具体实现连接到blueprint.xml从而不必使用@JsonDeseralize批注?

Environment 环境

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

Use a custom provider that extends JacksonJsonProvider in JAX-RS server definition within the <jaxrs:providers> tag. 使用自定义提供程序,该扩展程序<jaxrs:providers>标签内的JAX-RS服务器定义中扩展JacksonJsonProvider

Or use a custom deserializer that extends StdSerializer : 或使用扩展StdSerializer的自定义解串器:

@JsonDeserialize(using = YourDeserializer.java)

Or use polymorphism and @JsonTypeInfo + @JsonSubtypes annotations. 或使用多态和@JsonTypeInfo + @JsonSubtypes批注。

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

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