2

I am constructing the following JSON in my jQuery and sending to controller via ajax

var jsonResponse = [];

$(columns).each(function() {
     if(!isEmpty($(this))) {
          var lang = $(this).find(".language").val();
          var item = {
              "label": label,
              "language": $(this).find(".language").val(),
              "value": $(this).find(".value").val()
         };
         jsonResponse.push(item);
    }
 });

Since it is an array of individual items, I need to map it to an ArrayList in my controller.

My model objects are like this: FormModel - maps to the item JSON element

class FormModel {
    String label;
    String language;
    String value;
    // getters & setters
}

FormModelWrapper - maps to the array of items

    class FormModelWrapper {
    private List<FormModel> formModel;
    // getters & Setters
}

Finally the controller:

@RequestMapping(value = View Name, method = RequestMethod.POST)
public String performTask( @RequestBody FormModelWrapper wrapper) {

...
}

I have Jackson configured correctly. I have actually verified it by sending a JSON item element and accepting it in FormModel. The 415 Error occurs when I use array of json elements.

Any help would be appreciated.

AgentX
  • 1,402
  • 3
  • 23
  • 38
  • Turn on Spring DEBUG logging and see what it says. – Sotirios Delimanolis Sep 28 '15 at 15:55
  • Two questions: curious: Why would you need a mapper and isn't List as @RequestBody wouldn't work? 2. content-type - both accepted by controller and sent by client? – Praba Sep 28 '15 at 16:02
  • @prabugp I tried with List didn't work. Then I followed this:http://stackoverflow.com/questions/23012841/receiving-json-and-deserializing-as-list-of-object-at-spring-mvc-controller 2. sent by client 'application/json' I don't have anything over controoller though – AgentX Sep 28 '15 at 16:12
  • Please add more details about how you request the controller action from the Ajax Request?! and how you are passing the JSON format? – Husam Sep 28 '15 at 16:24
  • @prabugp it worked when I changed it back to List from FormModelWrapper. Need to figure out what I missed earlier. Thanks btw :) – AgentX Sep 28 '15 at 16:27
  • @Husam it worked somehow, I will post an answer with the code once I figure out how it worked. Maybe someone might benefit by stumbling upon it later. – AgentX Sep 28 '15 at 16:28
  • Glad it helped. :) please do post your answer as to what changed. – Praba Sep 28 '15 at 16:33

1 Answers1

0

I changed a lot of stuff here and there so I am not sure what was causing the error initially. Here is my solution that works:

jQuery:

var jsonResponse = [];

$(columns).each(function() {
 if(!isEmpty($(this))) {
      var lang = $(this).find(".language").val();
      var item = {
          "label": label,
          "language": $(this).find(".language").val(),
          "value": $(this).find(".value").val()
     };
     jsonResponse.push(item);
}
});

$.ajax({
        url: "actual_url",
        type: "POST",
        dataType: 'json',
        contentType:'application/json',
        data: JSON.stringify(jsonResponse),
        success: function(result) {
           alert(" Call Succeeded");
        },
        error: function(jqXHR, ajaxOptions, thrownError) {
            alert("Call Failed, " + thrownError);
        }
    });

Next, we have the form Model that maps to the item element in the jQuery

class FormModel {
    String label;
    String language;
    String value;
    // getters & setters
}

Here is the controller using the FormModel

@RequestMapping(value = viewName, method = RequestMethod.POST)
public String doTask( @RequestBody List<FormModel> data) {
    //your code here
}

I am using Jackson to do the JSON to POJO mapping

In my spring-context.xml, I have the Jackson View Resolver as default View for the View Resolver

<property name="defaultViews">
        <list>
            <bean
                    class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
        </list>
    </property>

Finally, the Jackson Configuration, this is also in servlet-context.xml

<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> </bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jacksonMessageConverter"/>
        </list>
    </property>
</bean>

This code works, atleast for me. Hope that helps someone.

AgentX
  • 1,402
  • 3
  • 23
  • 38