gpt4 book ai didi

java - 无法使用 REST API Controller 在服务器端检索 url 参数

转载 作者:行者123 更新时间:2023-11-29 05:00:09 25 4
gpt4 key购买 nike

我想从我的 REST API Controller 调用 REST API Controller 。

"http://localhost:8080/function/1?filter= {"id":1435263}"

由于 spring mvc 无法读取“{”,因此我们无法直接发送 ({"id":1435263})JSON 查询字符串和 url,因此我以 map 的形式发送查询字符串(搜索变量)。

Object response = restTemplate.getForObject(crudControllerURL,
Object.class, map);

其中 map 包含值。

在服务器端,我无法检索此 map 。我尝试了 @RequestParam Object obj 但它没有用。我不知道如何在那里获得这些值?

我需要将其转换为 POST 吗?

编辑

当我尝试将整个 url 与查询字符串一起使用时,我收到了

java.lang.IllegalArgumentException: Not enough variable values available to expand '"id"'

添加服务器端 Controller 代码片段(不完整),请注意我需要访问服务器 REST API Controller 中的 map 。服务器端 Controller

    @RequestMapping(value = "/{function}/{type}", method = RequestMethod.GET)
public List<Order> performFetchAll(@PathVariable String function,
HttpServletRequest request) throws JsonParseException,
JsonMappingException, IOException, InvalidAttributesException {
String requestQueryString = request.getQueryString();
if (requestQueryString == null
|| requestQueryString.equalsIgnoreCase(""))
return orderService.findAll();

请提供您的反馈。谢谢。

最佳答案

您应该这样做而不是使请求复杂化:

URL 可以这样改变: http://localhost:8080/function/1?idFilter=1435263

@RequestMapping(value = "/{function}/{type}", method = RequestMethod.GET)
public List<Order> performFetchAll(@PathVariable String function, @RequestParam("idFilter") String id, HttpServletRequest request) throws JsonParseException,
JsonMappingException, IOException, InvalidAttributesException {
//DO something
}

如果您的过滤请求将像 json 一样庞大而复杂,请将您的方法更改为 POST 并获取 JSON 并执行您的逻辑。

关于java - 无法使用 REST API Controller 在服务器端检索 url 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32374145/

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