[英]Wire concrete implementation to interface in blueprint.xml
希望将具体类连接到blueprint.xml
REST服务器的@POST
方法的参数。
Jackson数据绑定找不到方法的send
参数的具体实现。
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
批注?
使用自定义提供程序,该扩展程序在<jaxrs:providers>
标签内的JAX-RS服务器定义中扩展JacksonJsonProvider 。
或使用扩展StdSerializer的自定义解串器:
@JsonDeserialize(using = YourDeserializer.java)
或使用多态和@JsonTypeInfo
+ @JsonSubtypes
批注。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.