簡體   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