从@PathParam获取对象 [英] Getting object from @PathParam
问题描述
我正在创建用于访问数据的RESTful服务.
I'm creating a RESTful service for accessing data.
所以我开始编写该服务,首先我用以下代码创建了ReadOnlyResource
接口:
So I started writing that service, first I created a ReadOnlyResource
interface with the following code:
public interface ReadOnlyResource<E, K> {
Collection<E> getAll();
E getById(K id);
}
其中E是返回的类型,K是关键元素.
Where E is the returned type and K is the key element.
因此,如果我要使用<Integer, Integer>
实施,我将像t一样注入密钥
So if I'm implementing with <Integer, Integer>
I'll inject the key like t
@GET
@Path("/{id}")
@Override
public Integer getById(@PathParam("id") Integer id) {
return null;
}
但是当我的钥匙比较复杂时,就像这样:
But when my key is more complex, like this:
public class ComplexKey {
private String name;
private int value;
}
如何注入它以便可以使用我的界面?
How can I inject this so I can use my interface?
是否可以同时注入两个参数并使用它们创建密钥?
Is there a way to inject both params and create the key with them?
@QueryParam的解决方案无济于事,因为我尝试访问的是/一些名字/一些数字,并接收一个包含以下内容的ComplexKey实例:网址中的一些名称和一些数字值.
the solution of @QueryParam doesn't help because what I try to reach is going to /some name/some number and receive a ComplexKey instance which contains the some name and some number values from the url.
推荐答案
我尝试到达的位置是/一些名称/一些数字,并接收一个ComplexKey实例,该实例包含来自url的一些名称和一些数字值
what I try to reach is going to /some name/some number and receive a ComplexKey instance which contains the some name and some number values from the url
使用 @BeanParam
public class ComplexKey {
@PathParam("name")
private String name;
@PathParam("value")
private int value;
// getters/setters
}
@Path("/{name}/{value}")
getById(@BeanParam ComplexKey key)
这篇关于从@PathParam获取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!