Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON - Spring MVC : How to post json data to spring MVC controller

I have a problem posting JSON data from jsp to controller. Everytime I try I get an ajax error Bad Request. Im so new to JSON and I really don't know what I am doing wrong. I searched and tried some samples I can find in this site but still Im having a problem.

In my controller:

@RequestMapping (method = RequestMethod.POST, headers ={"Accept=application/json"}, value = "/form")
public String postJournalEntry (@RequestParam ("json") String json, Model model) {
    System.out.println(json);
    return "successfullySaved";
}

In my jsp:

$("#btnPostGlEntry").click(function () {
    var glEntries = '{"glEntries":[{"generalLedgerId":"1"},{"accountId":"4"},{"amount":"344.44"},{"description":"Test Entry"},{"debit":"Yes"}]}';
    $.ajax({
        type: "POST",
        contentType: "application/json",
        dataType: "json",
        url: contextPath + "/generalLedger/journalEntries/form",
        data : JSON.stringify(glEntries),
        success: function(data) {
            alert("Success!!!");
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(jqXHR + " : " + textStatus + " : " + errorThrown);
        }
    });
});

NOTE : Im not even sure if my function in my controller is correct. I think my controller and my ajax are wrong. Please help.

like image 724
NinjaBoy Avatar asked Jan 28 '13 06:01

NinjaBoy


People also ask

How pass JSON object in post request in spring boot?

Send JSON Data in POST Spring provides a straightforward way to send JSON data via POST requests. The built-in @RequestBody annotation can automatically deserialize the JSON data encapsulated in the request body into a particular model object. In general, we don't have to parse the request body ourselves.

How do you pass a JSON object into a method in Java?

Either use an export mapping to create a JSON string that you can pass to the Java action and then create a JSON object again from that string or just pass a root object to the Java and then in Java retrieve all the attached objects over the references to that root object.


2 Answers

If you want your JSON to be deserialized into some class, than you have to define method like this (and don't forget to add jsonConverter, as in previous answer):

.... method(@RequestBody MyClass data){ ... }

But, if you want your method to accept JSON as String than do this:

.... method(@RequestBody String json){ ... }

So, basically, if you post JSON, it means that JSON is not a parameter, it is body of the request. And eventually you have to use @RequestBody annotation, instead of @RequestParam.

You can find beautifull video tutorial of Spring Mvc and JSON here: sites.google.com/site/upida4j/example

like image 104
user2626270 Avatar answered Nov 03 '22 01:11

user2626270


it seems you dont have a Json Converter configured properly

like this one

<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>
like image 32
TheWhiteRabbit Avatar answered Nov 03 '22 01:11

TheWhiteRabbit