Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC : The request sent by the client was syntactically incorrect

When sending JSON requests to the server, I'm often greeted by this message:

The request sent by the client was syntactically incorrect ().

Usually it's an incorrect attribute that was passed that the controller didn't expect, since the object the JSON maps to doesn't contain it.

Finding the parameter is needlessly time consuming - is there a way to get more information, perhaps even a stack trace of the exception? I've tried running in debug mode and I'm using Jackson as my JSON (de)serialiser.

like image 697
Aram Kocharyan Avatar asked Oct 20 '12 17:10

Aram Kocharyan


3 Answers

To get more info / stack trace turn on debug logging for spring web in log4j.properties

log4j.logger.org.springframework.web=debug

like image 118
Farm Avatar answered Oct 15 '22 11:10

Farm


If the data that your consuming is from an external api and if you want to shield your controller from unnecessary elements/properties that you dont need you can use below annotation on POJO class

@JsonIgnoreProperties(ignoreUnknown = true) 

or you could set it globally

//jackson 2.0
jsonObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
like image 41
Sudhakar Avatar answered Oct 15 '22 13:10

Sudhakar


Old case, my be irrelevant by now, but for others with similar trouble I'll add this entry.

Check that you have double quotes, not single quotes. Also try escaping them.

I used the app curl for my test json requests.

My JSON was like this

{ 'name' : 'somedata', 'town':'some town' } 

and it just blew up in my eyes.

"The request sent by the client was syntactically incorrect" was the general error. So I changed it from single quotes to double quotes, no go. I then tried having a backslash infront of the single quotes, still no go. Then changed it to backslash infront of the double quotes and presto!

curl -i -H "Accept: application/json" -H "Content-Type: application/json" \ 
http://localhost:8077/dra/dump.json \ 
-X PUT -d "{\"name\" : \"My Name\", \"town\":\"My Town\"}" 
like image 31
OddBeck Avatar answered Oct 15 '22 11:10

OddBeck