The request sent by the client was syntactically incorrect (). + Spring, RESTClient

I am working with Spring MVC using JSON objects. while I'm trying to send a JSON Object from a RESTClient, I get

HTTP Status 400 - the request sent by the client was syntactically incorrect ().

This is my controller

ObjectMapper mapper=new ObjectMapper(); @RequestMapping(value = "/addTask", method = RequestMethod.GET) public ModelAndView addTask(@RequestParam("json") String json) throws JsonParseException, JsonMappingException, IOException { System.out.println("Json object from REST : "+json); Task task=(Task) mapper.readValue(json, Task); service.addService(task); return new ModelAndView("Result"); } 

My request url is: http://localhost:8080/Prime/addTask

My Json Object:

{"taskName": "nothing", "taskId": 1234, "taskDesc": "do nothing"}

I also tried to specify "Content-Type: application / json" in RESTClient, but still getting the same error

+8
json spring rest-client
source share
3 answers

try it

Change

 @RequestParam("json") String json 

For

  @RequestBody Task task 

If you are not interested in the POST method, you can try this

change your controller method from

 @RequestMapping(value = "/addTask", method = RequestMethod.GET) public ModelAndView addTask(@RequestParam("json") String json) 

to

 @RequestMapping(value = "/addTask/{taskName}/{taskId}/{taskDesc}", method = RequestMethod.GET) public ModelAndView addTask(@RequestParam("taskName") String taskName, @RequestParam("taskId") String taskId,@RequestParam("taskDesc") String taskDesc) 

and change your url to

 http://localhost:8080/Prime/addTask/mytask/233/testDesc 
+2
source share

I came across a similar situation using a JSON string in a request block recently and using a very similar Spring installation like yours. In my case, I did not specify the String parameter and deserialize it myself, although I let Spring do this:

  @RequestMapping(value = "/myService/{id}", method = RequestMethod.POST) @ResponseBody public void myService(@PathVariable(value = "id") Long id, @RequestBody MyJsonValueObject request) { .. } 

I was getting HTTP 400 error "The request sent by the client was syntactically incorrect." Until I realized that the default constructor was not @RequestBody MyJsonValueObject , so there were problems deserializing it. However, this problem is presented in this way.

So, if you use POST and objects and get such errors, make sure you have a default constructor ! Add JUnit to make sure you can deserialize this object.

Note. I am not saying that this is the only reason you get this error. In the original case, only String was used (which has a default constructor!), So it is slightly different. But in both cases, it appears that the request URI seems to have been mapped to the correct method, and something went wrong trying to extract the parameters from the HTTP request.

+16
source share

My problem is due to the incorrect display of the @RequestBody object.

The body of my request looks like

 {data: ["1","2","3"]} 

I had the following code in the controller

 @RequestMapping(value = "/mentee", method = RequestMethod.POST) public @ResponseBody boolean updateData(@RequestBody List<Integer> objDTO, HttpSession session) { ... } 

This gives me HTTP 400 because Spring doesn't know how to bind my Json data to a list.

I changed the RequestBody object to the next

 @RequestMapping(value = "/mentee", method = RequestMethod.POST) public @ResponseBody boolean updateData(@RequestBody ObjectiveDto objDTO, HttpSession session) { ... } 

and defined by ObjectiveDto as follows

 @ToString public class ObjectiveDto { @Getter @Setter private List<Integer> data; } 

This resolved the HTTP 400 error.

0
source share

All Articles