Search code examples
jsonspringrest-client

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


Solution

  • 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