使用Spring MVC中的curl将String值的数组作为请求主体发布

时间:2022-09-13 13:20:36

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

无法使用curl将字符串ID数组发布到Spring mvc控制器,因为它正在抛出400状态。

Controller Method:

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

CURL Commands:

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  

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

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

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

1 个解决方案

#1


1  

Got the solution:

得到了解决方案:

CURL command to post the String Array in request body :

CURL命令在请求体中发布字符串数组:

curl -H "Content-type: 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

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.

注意:1。控制器方法2中没有修改。如果控制器接受字符串ID列表作为请求主体,则可以使用相同的方法。

#1


1  

Got the solution:

得到了解决方案:

CURL command to post the String Array in request body :

CURL命令在请求体中发布字符串数组:

curl -H "Content-type: 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

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.

注意:1。控制器方法2中没有修改。如果控制器接受字符串ID列表作为请求主体,则可以使用相同的方法。