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

3

8

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

Nut answered 8/1, 2013 at 5:23 Comment(3)
I am not sure about how Spring works but if your request method is GET shouldn't you request URL be http://localhost:8080/Prime/addTask?json=%7B%22taskName%22%3A%22nothing%22%2C%22taskId%22%3A1234%2C%22taskDesc%22%3A%22nothing%20doing%22%7D with the json parameter sent in the query string?Pinhole
I also tried by adding the json string to request URL but still it says the same error.Nut
You can use a tool like Fiddler (fiddler2.com/fiddler2/version.asp) to check the request. Also post it in the question so others can have a look at it.Pinhole
G
3

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
Gottuard answered 8/1, 2013 at 5:43 Comment(2)
Thanks for your reply. I tried but no use. Also @RequestBody is for POST method. I am using GET methodNut
@Nut I dont think you can send object in get methodGottuard
D
16

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.

Desouza answered 2/10, 2013 at 6:39 Comment(1)
Why we have to have default constructor with POST?Boride
G
3

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
Gottuard answered 8/1, 2013 at 5:43 Comment(2)
Thanks for your reply. I tried but no use. Also @RequestBody is for POST method. I am using GET methodNut
@Nut I dont think you can send object in get methodGottuard
A
0

My problem was due to the incorrect mapping of the @RequestBody object.

My Request Body looks like this

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

I had the following code in my controller

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

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

I changed the RequestBody object to the following

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

and defined ObjectiveDto as followed

@ToString
public class ObjectiveDto {

    @Getter @Setter
    private List<Integer> data;

}

This resolved the HTTP 400 error.

Advise answered 24/2, 2015 at 19:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.