gpt4 book ai didi

java - 自定义 Jersey 参数解码

转载 作者:太空宇宙 更新时间:2023-11-04 14:52:58 25 4
gpt4 key购买 nike

我收到一个像

这样的字符串作为查询参数
parameter=123,456,789

我想要直接在我的 Controller 中获取List<Integer>。像这样的事情:

@GET
@Path(REST_PATH)
@Produces(MediaType.APPLICATION_JSON)
public Response getSomeStuff(@MagicalThing("parameter") List<Integer> requiredList)

除了自定义提供程序和附加类型之外还可以做什么:

https://stackoverflow.com/a/6124014

更新:自定义注释解决了http://avianey.blogspot.de/2011/12/exception-mapping-jersey.html的难题

最佳答案

没有内置机制可以执行此操作。您需要在您的方法或提供程序中自行拆分该字符串,或者在您自己的具有带字符串参数的构造函数的对象中,例如:

public Response getSomeStuff(@QueryParam("parameter") MyList requiredList) {
List<String> list = requiredList.getList();
}

MyList 可能在哪里:

 public class MyList {
List<String> list;
public MyList(Srting parameter) {
list = new ArrayList<String>(parameter.split(","));
}

public List<String> getList() {
return list;
}
}

然后用你的方法获取我的列表。

关于java - 自定义 Jersey 参数解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23541854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com