我正在探索 MatrixURIs 的使用在 JAX-RS 应用程序中。在此过程中,我注意到 @MatrixParam带注释的参数似乎只能从最后一段获取矩阵参数(这使得尝试使用 sub-resource locators 有点痛苦)。我想知道这是一个错误,还是规范的一部分(为什么?),或者仅仅是一个未定义的破解。
我知道我可能会想出一种方法来使用注入(inject)的@Context UriInfo对象(它确实会根据定位器链中的位置而改变),但这感觉很丑陋。
快速浏览 JAX-RS 1.1 spec ...我能找到的最接近不受支持的是:§3.2
Because injection occurs at object creation time, use of these annotations (with the exception of @Context) on resource class fields and bean properties is only supported for the default per-request resource class lifecycle.
但那是在谈论构造函数/字段注入(inject),而不是像 §3.3.2 中那样的方法参数:
When a resource method is invoked, parameters annotated with @FormParam or one of the annotations listed in section 3.2 are mapped from the request according to the semantics of the annotation.
当然,注解的语义似乎很模糊。
运行时环境详细信息:
- Jersey 1.13、1.15
- Tomcat 7.0.29
- Java 1.6.0_31(苹果)
- MacOS X 10.7.5
示例资源:
public class Zero {
public static final String[] IDS = { "1", "2", "3" };
@GET
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> getMe(@Context final UriInfo info) {
final UriBuilder builder = info.getAbsolutePathBuilder().path("one");
final UriBuilder two = builder.clone().matrixParam("id", "{one}", "{two}");
final List<String> links = new ArrayList<String>();
for (int i = 0; i < IDS.length; i++) {
final String first = IDS[i];
for (int j = i + 1; j < IDS.length; j++) {
final String second = IDS[j];
links.add(two.build(first, second).toASCIIString());
}
}
final Map<String, Object> toReturn = new HashMap<String, Object>();
toReturn.put("class", getClass().getSimpleName());
toReturn.put("next", builder.build().toASCIIString());
toReturn.put("skip", links);
return toReturn;
}
/*************************/
/**** <PROBLEM CHILD> ****/
/*************************/
@Path("one")
public Object getNext(@MatrixParam("id") final Set<String> ids) {
if (ids.isEmpty()) {
return new One();
}
return new Two(ids.toArray(new String[ids.size()]));
}
/*************************/
/**** </PROBLEM CHILD> ***/
/*************************/
public static class One {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> getMe(@Context final UriInfo info) {
final UriBuilder builder = info.getAbsolutePathBuilder().path("{id}");
final List<String> links = new ArrayList<String>();
for (final String id : IDS) {
links.add(builder.build(id).toASCIIString());
}
final Map<String, Object> toReturn = new HashMap<String, Object>();
toReturn.put("class", getClass().getSimpleName());
toReturn.put("next", links);
toReturn.put("last", getLastURI(info));
return toReturn;
}
@Path("{id}")
public Two getNext(@PathParam("id") final String id) {
return new Two(id);
}
}
public static class Two {
private final String[] myids;
private final Three three;
public Two(final String... ids) {
three = new Three(ids);
myids = ids;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> getMe(@Context final UriInfo info) {
final UriBuilder builder = info.getAbsolutePathBuilder().path("three");
final Map<String, Object> toReturn = new HashMap<String, Object>();
toReturn.put("class", getClass().getSimpleName());
toReturn.put("ids", myids);
toReturn.put("next", builder.build().toASCIIString());
toReturn.put("last", getLastURI(info));
return toReturn;
}
@Path("three")
public Three getNext() {
return three;
}
}
public static class Three {
private final String[] myids;
public Three(final String... ids) {
myids = ids;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> getMe(@Context final UriInfo info) {
final Map<String, Object> toReturn = new HashMap<String, Object>();
toReturn.put("class", getClass().getSimpleName());
toReturn.put("ids", myids);
toReturn.put("last", getLastURI(info));
return toReturn;
}
}
/**
* Helper method since info.getMatchedURIs doesn't play nice with Matrix Params
* @param info info object
* @return parent URI
*/
public static final String getLastURI(final UriInfo info) {
final List<PathSegment> segments = info.getPathSegments(false);
final UriBuilder builder = info.getBaseUriBuilder();
for (int i = 0; i < segments.size() - 1; i++) {
final PathSegment segment = segments.get(i);
builder.path(segment.getPath());
final MultivaluedMap<String, String> matrixParams = segment.getMatrixParameters();
if (!matrixParams.isEmpty()) {
for (final Map.Entry<String, List<String>> param : matrixParams.entrySet()) {
final String name = param.getKey();
final String[] values = param.getValue().toArray(new String[param.getValue().size()]);
builder.matrixParam(name, values);
}
}
}
return builder.build().toASCIIString();
}
}
上面的示例输出:
http://localhost:8080/context/zero/one/2
{
"last":"http://localhost:8080/context/zero/one",
"ids":["2"],
"next":"http://localhost:8080/context/zero/one/2/three",
"class":"Two"
}
http://localhost:8080/context/zero/one/2/three
{
"last":"http://localhost:8080/context/zero/one/2",
"ids":["2"],
"class":"Three"
}
http://localhost:8080/context/zero/one;id=1;id=2
{
"last":"http://localhost:8080/context/zero",
"ids":["2","1"],
"next":"http://localhost:8080/context/zero/one;id=1;id=2/three",
"class":"Two"
}
http://localhost:8080/context/zero/one;id=1;id=2/three
{
"last": "http://localhost:8080/context/zero/one;id=1;id=2",
"ids": ["three"],
"next": "http://localhost:8080/context/zero/one;id=1;id=2/three/three",
"class": "Two"
}
最佳答案
我不能肯定地说,但我怀疑这是设计使然。假设您有一个像这样的二级 URI:
/foo/bar
在两个级别上都可以有一个名为 filter 的矩阵参数。您最终可能会得到这样的 URL:
/foo;filter=13/bar;filter=April
在该路径的 Jersey 资源中,您将有一个用 @MatrixParam("filter") 注释的参数。 Jersey 应该填写哪个过滤器参数?如果其中一个未提供怎么办?
刚刚实现了一些矩阵参数,我可以看到当前实现中的值。指定 @MatrixParam 仅拉取最后一层。如果您想在 URI 的前面使用矩阵参数,请指定 @PathParameter 并将参数类型设置为 PathSegment。您可以从路径段中提取矩阵参数。
通常,资源类会有一个全局@Path,每个方法都有自己的@Path 添加到类级别设置中。在这种情况下,@MatrixParameter 仅适用于最后一个路径段似乎更有意义。
因此,将所有这些放在一起(对于我们上面的示例)会产生以下结果
@Path("{fooPath: foo}")
public class FooResource {
@GET
@Path("bar")
public String getFooBar(@PathParam("fooPath") PathSegment fooPath,
@MatrixParam("filter") String filter) {
MultivaluedMap<String, String> mParms = fooPath.getMatrixParameters();
List<String> parms = mParms.get("filter");
String fooFilter = parms.get(0);
// ... the rest of your code
}
}
关于java - 为什么 @MatrixParam 值似乎只来自最后一段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13169890/