2017-04-03 113 views
1

我正在創建一個用於訪問數據的RESTful服務。從@PathParam獲取對象

於是我開始寫的是服務,首先我創建了一個ReadOnlyResource接口與下面的代碼:

​​

其中E爲返回類型,K是關鍵因素。

所以,如果我跟<Integer, Integer>實現,我會注入樣T

@GET 
@Path("/{id}") 
@Override 
public Integer getById(@PathParam("id") Integer id) { 
    return null; 
} 

但關鍵的時候,我的關鍵是更復雜的,就像這樣:

public class ComplexKey { 
    private String name; 
    private int value; 
} 

我怎麼能注入該所以我可以使用我的界面?

有沒有辦法注入兩個參數,並與他們創建密鑰?

編輯:@QueryParam的解決方案並沒有幫助,因爲我努力達成打算/ 一些名/一定數量和接收包含了一些名字,並從一些數字值的ComplexKey實例網址。

+0

可能重複[傳遞自定義類型查詢參數](http://stackoverflow.com/questions/30403033/passing-custom-type-query-parameter) – zloster

+0

不完全,當我想要的是一種我可以用當前接口實現的方式,必須是傳遞給方法的1個參數。 – TalOhana

回答

3

我努力達成是要/一些名稱/一定數量和接收包含了一些名稱和鏈接的一些數字值的ComplexKey實例

使用@BeanParam

public class ComplexKey { 
    @PathParam("name") 
    private String name; 
    @PathParam("value") 
    private int value; 
    // getters/setters 
} 

@Path("/{name}/{value}") 
getById(@BeanParam ComplexKey key)