Reputation: 14658
I have following Spring controller mapping:
@RequestMapping(value="/isSomethingHappening", method = RequestMethod.POST)
public @ResponseBody JsonResponse isSomethingHappening(HttpServletRequest httpRequest,@RequestParam("employeeId") String employeeId,
ModelMap model) throws IOException{
If I invoke this as below then I get 400 response.
var requestData = {"employeeId":XYZ.application.employeeId};
XYZ.network.fireAjaxRequestAsync("application/json", "forms/testing/isSomethingHappening", requestData, function(response, status, xhr){
But if I invoke this as below then I get success response.
var requestData = {"employeeId":XYZ.application.employeeId};
XYZ.network.fireAjaxRequestAsync("application/x-www-form-urlencoded", "forms/testing/isSomethingHappening", requestData, function(response, status, xhr){
I figured the fix but I am not able to understand why first one gave me error when my request data object var requestData = {"employeeId":XYZ.application.employeeId};
remained unchanged and I just changed the content type. To me application/json
looks more appropriate content type because my request data is a JSON object.
@RequestMapping(value = "/getOnFlyResults", method = RequestMethod.POST)
public @ResponseBody JsonResponse getOnFlyResults(HttpServletRequest httpRequest,
@RequestBody testingRequestVO testingRequestVO, ModelMap modelMap) throws IOException{
And for invoking this, I send request as below:
var requestData = {"employeeId":XYZ.application.employeeId,
"fName":XYZ.application.fName,
"lName": XYZ.application.lName,
"telephoneNumber":telephoneNumber,
"testMode":XYZ.constant.onFly};
XYZ.network.fireAjaxRequestAsync("application/json", "forms/testing/startTest", JSON.stringify(requestData), function(response, status, xhr){
I don't understand that why I have to stringify the data using JSON.stringify(requestData)
, if I do not do this then I will get 400.
Once I stringify then it becomes a string then my content type should have been text/plain
but it works with application/json
Please note that I know the code fixes, but I want to understand the concept. I have read this and it doesn't explain the concept in details and queries I have.
Upvotes: 0
Views: 783
Reputation: 7867
It is common to use Jackson library in your Spring application to process JSON. If you are using Ant, try adding Jackson to your libraries.
You can download the Jackson library directly from Maven Central. Here is an example Maven dependency block (but get the latest version):
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
Make sure you have the annotation-driven tag in your Spring configuration:
<annotation-driven />
With or without Jackson, it may also help to specify that the controller endpoint produces or consumes JSON;
@RequestMapping(value="/getjson",
produces=MediaType.APPLICATION_JSON_VALUE, consumes=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody MyObject getJSON(){
return new MyObject();
}
Upvotes: 1