Reputation: 5664
When i used @RequestBody and pass json from frontend by AJAX with all the parameters of that object it works.
For Ex: @RequestBody Person person
Where person is a bean that contains name and phone info. So when i pass the json with name and phone info from frontend it works fine.
But it doesnt work when i add another parameter like post_id in json which is not related to person object. It gives me error stating "the request sent by the client was syntactically incorrect ()"
Note : i also added consumes = "application/json" and produces = "application/json" in spring and in jquery ajax i added contentType : "application/json"
Upvotes: 1
Views: 832
Reputation: 49915
You have to set a parameter on the ObjectMapper used by Jackson:
objMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
This is one way of setting this in Spring MVC:
Define a Custom Object Mapper this way:
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
public class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper(){
super.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
super.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
}
Register this custom ObjectMapper:
<mvc:annotation-driven >
<mvc:message-converters register-defaults="false">
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
<property name="objectMapper">
<bean class="....CustomObjectMapper"/>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
Also, in your request you will need to have an "Accept" header of "application/json"
Upvotes: 1