相关疑难解决方法(0)

JQuery,Spring MVC @RequestBody和JSON - 让它协同工作

我想有一个双向JSON到Java序列化

我正在成功使用Java到JSON到JQuery路径......(@ResponseBody)例如

@RequestMapping(value={"/fooBar/{id}"}, method=RequestMethod.GET)
     public @ResponseBody FooBar getFooBar(
            @PathVariable String id,
            HttpServletResponse response , ModelMap model) {
        response.setContentType("application/json");
...
}
Run Code Online (Sandbox Code Playgroud)

在我使用的JQuery中

$.getJSON('fooBar/1', function(data) {
    //do something
});
Run Code Online (Sandbox Code Playgroud)

很有效(例如注释工作已经完成,感谢所有的回答者)

但是,如何执行反向路径:使用RequestBody将JSON序列化为Java对象?

无论我尝试什么,我都无法得到这样的东西:

@RequestMapping(value={"/fooBar/save"}, method=RequestMethod.POST)
public String saveFooBar(@RequestBody FooBar fooBar,
        HttpServletResponse response , ModelMap model) {

  //This method is never called. (it does when I remove the RequestBody...)
}
Run Code Online (Sandbox Code Playgroud)

我已经正确配置了Jackson(它在出路时序列化)并且当然我将MVC设置为注释驱动

我如何使其工作?它有可能吗?或者Spring/JSON/JQuery是单向的(out)?


更新:

我改变了杰克逊的设定

<bean id="jsonHttpMessageConverter"
    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

<!-- Bind the return value of the Rest …
Run Code Online (Sandbox Code Playgroud)

java jquery spring json spring-mvc

70
推荐指数
4
解决办法
17万
查看次数

将JSON映射传递给Spring MVC Controller

我正在尝试将Map的JSON表示作为POST参数发送到我的控制器中.

@RequestMapping(value = "/search.do", method = RequestMethod.GET, consumes = { "application/json" })
public @ResponseBody Results search(@RequestParam("filters") HashMap<String,String> filters, HttpServletRequest request) {
       //do stuff
}
Run Code Online (Sandbox Code Playgroud)

我发现@RequestParam只会抛出500错误,所以我尝试使用@ModelAttribute.

@RequestMapping(value = "/search.do", method = RequestMethod.GET, consumes = { "application/json" })
public @ResponseBody Results search(@ModelAttribute("filters") HashMap<String,String> filters, HttpServletRequest request) {
       //do stuff
}
Run Code Online (Sandbox Code Playgroud)

这将正确响应请求,但我意识到地图是空的.随后的实验,我发现任何对象(不仅仅是HashMap)都会被实例化,但是不会填充任何字段.我的类路径上有Jackson,而我的控制器将使用JSON进行响应.但是,我的当前配置似乎不允许Spring通过GET/POST参数读取JSON.

如何将客户端AJAX请求中的对象的JSON表示作为请求参数传递给Spring控制器并获取Java对象?

编辑添加我的相关Spring配置

  <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
      <map>
        <entry key="html" value="text/html" />
        <entry key="json" value="application/json" />
      </map>
    </property>
    <property name="viewResolvers">
      <list>
        <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
          <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
          <property name="prefix" value="/WEB-INF/jsp/" /> …
Run Code Online (Sandbox Code Playgroud)

java spring json spring-mvc

17
推荐指数
1
解决办法
7万
查看次数

使用Spring的JQuery/Ajax的JSON请求

我是JSON/Spring MVC的新手,我正在尝试获得一个关于Spring MVC控制器的AJAX调用的简单示例 - 但我一直返回错误400 - 错误请求.

在浏览互联网之后,我发现这通常是由于没有设置适当的内容类型 - 但[我相信]我已经这样做了.

这是我的AJAX调用:

    //validate the object
    var urlString = "/ajax/add/";

    $.ajax({
        type:"POST",
        url: urlString,
        contentType: "application/json; charset=utf-8",
        dataType: "json", 
        data: {value1: 'apples', value2 : 'oranges'},
        success: function(result){
            alert("success");
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert("error:" + textStatus + " exception:" + errorThrown);
        }

    }) ;
Run Code Online (Sandbox Code Playgroud)

而我的控制器:

@Controller
itpublic class AddController {

private static Logger m_log = null;

@RequestMapping(value = "/ajax/add")
public String handle(@RequestBody String json){

    DummyClass dummyRequest;
    try {

        ObjectMapper mapper = new …
Run Code Online (Sandbox Code Playgroud)

ajax jquery json spring-mvc

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

json ×3

spring-mvc ×3

java ×2

jquery ×2

spring ×2

ajax ×1