[英]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.