2016-08-03 46 views
0

我想将多个参数传入我的方法。我该怎么做呢?我想要的网址看起来像这样http://host/one/two/three/fourJava中的多个参数Rest API JAX-RS - GET方法

我有下面的代码到目前为止

@GET 
@Produces({MediaType.APPLICATION_JSON}) 
@Path("/{one,two,three}") 

public List<Person> getPeople(@PathParam ("one") String one, @PathParam ("two") String two, @PathParam ("three") String three){ 
    String one = one; 
    String two = two; 
    String three = three; 

} 

这是为抓住PARAMS并将它传递给我的方法正确的语法?我在@Path中看到了一些正则表达式,但我不明白。我老实说,只是希望能够抓住参数,并尽可能将它们放入一个变量中。

+1

你要求的参数不确定数量还是有一个固定的号码吗?你在你的例子中通过3来展示,但是在你的例子url中通过4来展示。 – gregwhitaker

回答

4

定员的路径参数:

@GET 
@Path("/{one}/{two}/{three}") 
@Produces(MediaType.APPLICATION_JSON) 
public Response foo(@PathParam("one") String one, 
        @PathParam("two") String two, 
        @PathParam("three") String three) { 

    ... 
} 

可变数量的路径参数:

@GET 
@Path("/{path: .+}") 
@Produces(MediaType.APPLICATION_JSON) 
public Response foo(@PathParam("path") String path) { 

    String[] paths = path.split("/"); 

    ... 
} 
+2

@Luke检查[documentation](https://jersey.java.net/documentation/latest/jaxrs-resources.html#d0e2193)。 –

+1

非常感谢! – Luke