Post Array of String values as request body using

2019-07-14 16:39发布

问题:

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

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

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

回答1:

Got the solution:

CURL command to post the String Array in request body :

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.