gpt4 book ai didi

json - 将json对象发送到spring MVC中的GET方法

转载 作者:行者123 更新时间:2023-12-01 03:39:10 25 4
gpt4 key购买 nike

我有一个从给定表/data_type 中获取所有对象的端点。我想对返回的数据添加一些过滤功能,但无法弄清楚如何将 JSON 对象传递给我的 Controller 。

我的代码:

    @RequestMapping(value = "/{dataType}.json", method = RequestMethod.GET)
public @ResponseBody List findAll(@PathVariable String dataType, @RequestParam(required=false) Map<String, Object> query) {
}

如何将数据传递给查询参数?我尝试了@ModelAttribute 并在请求正文中发送了 JSON 对象,但它不起作用。

请帮我解决这个问题

最佳答案

您不能直接在请求参数上发送 JSON。从文档:

When an @RequestParam annotation is used on a Map or MultiValueMap argument, the map is populated with all request parameters.



我很确定您需要在要传递给服务器的 json 结构上执行诸如调用 encodeURIComponent() 之类的操作,然后将参数设为字符串。在服务器端,您可以使用 jersey 或其他东西将字符串转换回您可以操作的内容。

这篇文章可能会提供更多见解:

Spring MVC: Complex object as GET @RequestParam

关于json - 将json对象发送到spring MVC中的GET方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32077382/

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