我已经定义了如下所述的 GET 方法:
@GET
@Produces("text/plain")
@Path("method/{id}/{value}")
public void method(@PathParam("ids") final String id, @PathParam("value") final String value) {}
它允许我传递一个 id
和 value
。如何将一堆 id/value
对传递给此方法?
最佳答案
一种方法是创建您自己的协议(protocol),如下所示:
method/1,2,3,4,5,6,7/one,two,three,four,five,six,seven
然后,在您的代码中解析“协议(protocol)”:
public void method(@PathParam("ids") final String idStr, @PathParam("value") final String valueStr) {
String[] ids = idStr.split(",");
String[] values = valueStr.split(",");
}
但是很丑
第二种是使用 POST 正文,在那种情况下你不能使用 GET,你应该创建一个 ValuesMapObject,它保存每个 id 到它的值之间的映射并从客户端发送它,在这种情况下函数将如下所示:
@POST
@Produces("text/plain")
@Consumes(MediaType.APPLICATION_XML)
@Path("method/ids_and_values")
public void method(ValuesMapObject values) {}
关于java - 通过 JAX-RS 参数传递键值列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11561561/