我需要将带有多个可选参数的路径映射到我的端点
路径看起来像localhost/func1/1/2/3
或localhost/func1/1
或localhost/func1/1/2
,此路径应与
public Double func1(int p1, int p2, int p3){
...
}
我的注释应该使用什么?
与Jersey一起玩的测试任务是找到使用多个可选参数的方法,而不是学习REST设计。
答案 0 :(得分:8)
要解决此问题,您需要选择params,还需/
签署可选
在最终结果中,它看起来与此相似:
@Path("func1/{first: ((\+|-)?\d+)?}{n:/?}{second:((\+|-)?\d+)?}{p:/?}{third:((\+|-)?\d+)?}")
public String func1(@PathParam("first") int first, @PathParam("second") int second, @PathParam("third") int third) {
...
}
答案 1 :(得分:6)
您应该尝试 QueryParams :
@GET
@Path("/func1")
public Double func1(@QueryParam("p1") Integer p1,
@QueryParam("p2") Integer p2,
@QueryParam("p3") Integer p3) {
...
}
请求如下:
localhost/func1?p1=1&p2=2&p3=3
这里所有参数都是可选的。在路径部分内,这是不可能的。服务器无法区分例如:
func1/p1/p3
和func1/p2/p3
以下是 QueryParam 用法的一些示例:http://www.mkyong.com/webservices/jax-rs/jax-rs-queryparam-example/。