Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am working with Spring MVC using JSON objects. while I am tring to send JSON Object from RESTClient, I am getting

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 : http://localhost:8080/Prime/addTask

My Json Object :

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

Also i tried specifying "Content-Type: application/json" in RESTClient but still am getting the same error

like image 921
Chandrasekar Avatar asked Jan 08 '13 05:01

Chandrasekar


2 Answers

I ran into a similar situation using a JSON string in the request body recently, and using a very similar Spring setup as yours. In my case I wasn't specifying a String parameter and deserialising it myself though, I was letting Spring do that:

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

I was getting an HTTP error 400 "The request sent by the client was syntactically incorrect" response. Until I realised that there wasn't a default constructor on the @RequestBody MyJsonValueObject so there were problems deserialising it. That problem presented in this way though.

So if you are using POST and objects, and getting errors like this, make sure you have a default constructor! Add some JUnit to be sure you can deserialise that object.

Note: I'm not saying this is the only reason you get this error. The original case used just String (which does have a default constructor !) so it's a little different. But in both cases it appears the request URI appears to have been mapped to the right method, and something has gone wrong trying to extract parameters from the HTTP request.

like image 73
Ben Iggulden Avatar answered Oct 12 '22 00:10

Ben Iggulden


Try this

Change

@RequestParam("json") String json

To

 @RequestBody Task task

If you are not interested in 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
like image 41
Kris Avatar answered Oct 12 '22 01:10

Kris