在Spring MVC中使用curl将字符串值数组发布为请求正文 [英] Post Array of String values as request body using curl in Spring MVC

查看:328
本文介绍了在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"]}' http://localhost:8080/vt/evidencesbyIds 

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

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

curl  -H "Content-type: application/json"   -X POST -d "{\"ids\":[\"1\",\"2\"]}" http://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]" http://localhost:8080/vt/evidencesbyIds

curl -H "Content-type: application/json" -X POST -d "["1","2"]" http://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将字符串值数组发布为请求正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆