我正在探索在 JAX-RS 应用程序中使用MatrixURI 。在此过程中,我注意到@MatrixParam注释参数似乎只能从最后一段获取矩阵参数(这使得尝试使用子资源定位器有点痛苦)。我想知道这是一个错误,还是规范的一部分(为什么?),或者只是一个未定义的裂缝。
我知道我可能会设计一种方法来使用 @Context 注入的UriInfo对象(它会根据在定位器链中的位置而改变),但这感觉很难看。
快速浏览JAX-RS 1.1 规范......我能找到的最接近这个不受支持的是:§3.2
由于注入发生在对象创建时,因此仅在默认的每个请求资源类生命周期中支持在资源类字段和 bean 属性上使用这些注释(@Context 除外)。
但这是在谈论构造函数/字段注入,而不是 §3.3.2 中的方法参数:
当调用资源方法时,使用@FormParam 或3.2 节中列出的注释之一注释的参数根据注释的语义从请求中映射。
但是,注释的语义当然看起来很模糊。
运行时环境详细信息:
- 泽西 1.13, 1.15
- 雄猫 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"
}