Java Rest API JAX-RS中的多个参数 - GET方法

Hel*_*rld 0 java rest tomcat jax-rs

我想将多个参数传递给我的方法.我该怎么做呢?我希望网址看起来像这样的http:// host/one/two/three/four

到目前为止,我有以下代码

@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;

}
Run Code Online (Sandbox Code Playgroud)

这是抓住params并将其传递给我的方法的正确语法吗?我见过@Path中使用的一些正则表达式,但我不明白.老实说,我真的只想抓住参数并尽可能将它们放入变量中.

cas*_*lin 5

固定数量的路径参数:

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

    ...
}
Run Code Online (Sandbox Code Playgroud)

可变数量的路径参数:

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

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

    ...
}
Run Code Online (Sandbox Code Playgroud)

  • @Luke检查[文档](https://jersey.java.net/documentation/latest/jaxrs-resources.html#d0e2193). (2认同)