我想将多个参数传递到我的方法中。我该怎么做呢?我希望 url 看起来像这样 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;
}
这是获取参数并将其传递给我的方法的正确语法吗?我见过 @Path 中使用的一些正则表达式,但我不明白。老实说,我真的只是希望能够获取参数并在可能的情况下将它们放入变量中。
最佳答案
固定数量的路径参数:
@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("/");
...
}
关于java - Java Rest API JAX-RS 中的多个参数 - GET 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38745745/