将 json 对象发送到 spring MVC 中的 GET 方法
sending json object to GET method in spring MVC
我有一个从给定 table/data_type 获取所有对象的终点。我想对返回的数据添加一些过滤功能,但无法弄清楚如何将 JSON 对象传递给我的控制器。
我的代码:
@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 或其他工具将字符串转换回您可以操作的内容。
此 post 可能会提供更多见解:
Spring MVC: Complex object as GET @RequestParam
我有一个从给定 table/data_type 获取所有对象的终点。我想对返回的数据添加一些过滤功能,但无法弄清楚如何将 JSON 对象传递给我的控制器。
我的代码:
@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 或其他工具将字符串转换回您可以操作的内容。
此 post 可能会提供更多见解:
Spring MVC: Complex object as GET @RequestParam