在Spring MVC中使用curl将字符串值数组发布为请求正文

编程入门 行业动态 更新时间:2024-10-26 10:33:45
本文介绍了在Spring MVC中使用curl将字符串值数组发布为请求正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

由于curl抛出400状态,因此无法使用curl将字符串ID数组发布到Spring mvc控制器.

Not able to post Array of string ids to Spring mvc controller using curl as it is throwing 400 status.

控制器方法:

@RequestMapping(value="/evidencesbyIds",method = RequestMethod.POST) @ResponseBody public Map<String, Object> getEvidenceByIds(@RequestBody String[] ids){ // body here }

CURL命令:

curl -H "Content-type: application/json" -X POST -d '{"ids":["1","2"]}' localhost:8080/vt/evidencesbyIds curl -H "Content-type: application/json" -X POST -d "{"ids":["1","2"]}" localhost:8080/vt/evidencesbyIds curl -H "Content-type: application/json" -X POST -d "{"ids":[\"1\",\"2\"]}" localhost:8080/vt/evidencesbyIds curl -H "Content-type: application/json" -X POST -d "{\"ids\":[\"1\",\"2\"]}" localhost:8080/vt/evidencesbyIds

我多次尝试执行curl命令,但它们的状态为400,异常为

I tried multiple times to execute curl commands but they are throwing status as 400 and exception as

"Can not deserialize instance of java.lang.String[] out of START_OBJECT token\n at"

推荐答案

解决方案:

CURL命令将字符串数组发布到请求正文中:

CURL command to post the String Array in request body :

curl -H内容类型:application/json" -X POST -d"[" 1," 2]" localhost:8080/vt/evidencesbyIds

curl -H "Content-type: application/json" -X POST -d "["1","2"]" localhost:8080/vt/evidencesbyIds

注意: 1.控制器方法无修改 2.如果控制器接受String ID列表作为请求正文,则可以使用相同的方法.

Note: 1. No Modification in controller method 2. The same can be used to if the controller accept List of String ids as request body.

更多推荐

在Spring MVC中使用curl将字符串值数组发布为请求正文

本文发布于:2023-10-11 13:49:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1481874.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串值   正文   Spring   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!