2017-04-03 124 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)