JSON - Spring MVC:如何将json数据发布到spring MVC控制器

时间:2021-07-31 18:02:45

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.

我在将jsp数据从jsp发布到控制器时遇到问题。每次我尝试我得到一个ajax错误坏请求。我是JSON的新手,我真的不知道我做错了什么。我搜索并尝试了一些我可以在这个网站找到的样本,但我仍然有问题。

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:

在我的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.

注意:我甚至不确定我的控制器中的功能是否正确。我认为我的控制器和我的ajax是错误的。请帮忙。

2 个解决方案

#1


10  

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):

如果你想将你的JSON反序列化到某个类中,那么你必须定义这样的方法(并且不要忘记添加jsonConverter,如前面的答案):

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

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

但是,如果您希望您的方法接受JSON作为String而不是这样做:

.... 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.

所以,基本上,如果你发布JSON,这意味着JSON不是一个参数,它是请求的主体。最后你必须使用@RequestBody注释,而不是@RequestParam。

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

你可以在这里找到Spring Mvc和JSON的精彩视频教程:sites.google.com/site/upida4j/example

#2


4  

it seems you dont have a Json Converter configured properly

看来你没有正确配置Json转换器

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>

#1


10  

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):

如果你想将你的JSON反序列化到某个类中,那么你必须定义这样的方法(并且不要忘记添加jsonConverter,如前面的答案):

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

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

但是,如果您希望您的方法接受JSON作为String而不是这样做:

.... 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.

所以,基本上,如果你发布JSON,这意味着JSON不是一个参数,它是请求的主体。最后你必须使用@RequestBody注释,而不是@RequestParam。

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

你可以在这里找到Spring Mvc和JSON的精彩视频教程:sites.google.com/site/upida4j/example

#2


4  

it seems you dont have a Json Converter configured properly

看来你没有正确配置Json转换器

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>