【问题标题】:Mapping methods in Jersey frameworkJersey 框架中的映射方法
【发布时间】:2011-07-22 23:46:55
【问题描述】:
是否可以使用 ID 和参数将方法映射到对 URL 路径的方法调用?例如:
http://localhost/ws/updateUser/32332?name=John?psw=123
public void updateUser(Sting name, String psw){..}
当前的@PathParam 注解似乎只支持路径中的参数,例如:
http://localhost/ws/updateUser/32332/John/123
【问题讨论】:
标签:
java
web-services
jersey
【解决方案1】:
尝试使用@QueryParam捕获name和psw参数:-
public void updateUser(@QueryParam Sting name, @QueryParam String psw) {
..
}
【解决方案2】:
您可以将@QueryParam 和@PathParam 组合在一种方法中:
@GET
@Path("/user/{userId}")
public ShortLists getShortListsOfUser(@PathParam("userId") String userId,
@QueryParam("pageNumber") Integer pageNumber,
@QueryParam("pageSize") Integer pageSize,
@Context UriInfo uriInfo) {
/*do something*/
}
此方法匹配http://localhost/api/user/222?pageNumber=1&pageSize=3
使用 UriBuilder 运行此方法时,请注意使用 queryParam:
URI uri = getBaseUriBuilder().path("/user/user111").queryParam("pageSize", 2)
.queryParam("pageNumber", 3).build();
这不起作用:getBaseUriBuilder().path("/user/user111?pageSize=2&pageNumber=3").build();
(因为问号被builder替换成了%3F)