0

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.


Also, I have other instances where my controller mapping is as below:
@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.

hagrawal7777
  • 14,103
  • 5
  • 40
  • 70

1 Answers1

1

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();
}
pczeus
  • 7,709
  • 4
  • 36
  • 51
  • Thanks buddy for your response. I said that I fixed all the issues related to all this, I am more interested in concepts. – hagrawal7777 Mar 31 '16 at 01:25
  • If you are interested in concepts, I would highly recommend taking a peek at Spring Boot. If you look at the getting started page, you will see how easy it is to get up and running with a REST webservice: http://projects.spring.io/spring-boot/ – pczeus Mar 31 '16 at 01:27
  • That would explain how to do things in Spring, it wouldn't explain the concepts I am looking for. – hagrawal7777 Mar 31 '16 at 01:31