簡體   English   中英

球衣休息:如何返回接口而不是上課

[英]jersey rest: how to return interface instead of class

我有一個休息的網絡服務。 由於某些原因,我不想返回需要轉換為接口的類。 我想返回接口,並且在JSON中,我只想查看methods(開始獲取)值。 並非來自已實現類的所有屬性。

舉個例子:

@Path("getValues/")
@GET   
@Produces( MediaType.APPLICATION_JSON)
public DetailInterface getClientDetail() {
   return new DetailImpl(); 
}

並考慮接口:

public interface DetailInterface { 
    public String getName();
    public String getAge();
} 

並考慮實施

public class DetailImpl implements DetailInterface {
    public String getName() 
       return "my name";
    }

    public String getAge(){
       return "my age";
    }

    public String iDontWantThisinJSON() {
        return "I don't want this in JSON output";
    }
}

當我請求其余服務時,我看到iDontWantThisinJSON屬性也出現在JSON響應中。 我不想在響應中包括它。

我怎樣才能解決這個問題?

您是否嘗試過@JsonIgnore批注? 將字段標記為transient可能也有幫助-但是我不確定是否可以。

這比Rest或Json更像是Java概念。 您將必須按以下方式修改代碼,並且該代碼應該可以按您希望的方式工作。

@Path("getValues/")
    @GET   
    @Produces( MediaType.APPLICATION_JSON)
    public DetailInterface getClientDetail() {
       DetailInterface di = new DetailImpl()
       return di; 
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM