【问题标题】:Multiple params in Java Rest API JAX-RS - GET MethodJava Rest API JAX-RS 中的多个参数 - GET 方法
【发布时间】:2016-08-03 13:58:18
【问题描述】:

我想将多个参数传递给我的方法。我该怎么做呢?我希望网址看起来像这样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 中使用了一些正则表达式,但我不明白。老实说,我真的只是希望能够获取参数并尽可能将它们放入变量中。

【问题讨论】:

  • 您要求的参数数量是不确定的还是有固定数量的?您在示例中显示为 3,但在示例 url 中显示为 4。

标签: java rest tomcat jax-rs


【解决方案1】:

固定数量的路径参数:

@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("/");

    ...
}

【讨论】:

猜你喜欢
  • 2016-12-18
  • 1970-01-01
  • 2017-11-12
  • 1970-01-01
  • 1970-01-01
  • 2015-04-25
  • 1970-01-01
  • 1970-01-01
  • 2016-05-05
相关资源
最近更新 更多