将具体实现连接到 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
注解?
环境
- Java1.8
- Apache 骆驼 2.x
- JAX-RS 2.1
- Jackson JAX-RS JSON 提供商 2.9.x
- OSGi 1.2.1
- 不使用Spring
在 <jaxrs:providers>
标记内的 JAX-RS 服务器定义中使用扩展 JacksonJsonProvider 的自定义提供程序。
或者使用扩展 StdSerializer:
的自定义反序列化器
@JsonDeserialize(using = YourDeserializer.java)
或者使用多态和@JsonTypeInfo
+ @JsonSubtypes
注解
背景
希望将具体 class 连接到 blueprint.xml
.
@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
注解?
环境
- Java1.8
- Apache 骆驼 2.x
- JAX-RS 2.1
- Jackson JAX-RS JSON 提供商 2.9.x
- OSGi 1.2.1
- 不使用Spring
在 <jaxrs:providers>
标记内的 JAX-RS 服务器定义中使用扩展 JacksonJsonProvider 的自定义提供程序。
或者使用扩展 StdSerializer:
的自定义反序列化器@JsonDeserialize(using = YourDeserializer.java)
或者使用多态和@JsonTypeInfo
+ @JsonSubtypes
注解