相关疑难解决方法(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万
查看次数

Spring MVC可以为HTTP PUT方法提供请求参数,还是我必须使用post?我应该使用哪个RESTful?

我有一个控制器动作,我认为应该是一个HTTP PUT,但是当我尝试在控制器动作中使用@RequestParam时,Spring正在抱怨.HTTP PUT方法不允许使用请求参数,这就是Spring拒绝它的原因吗?

@RequestMapping(value = "/{helpDocumentId}/vote", method = RequestMethod.PUT)
public void voteHelpfulness(@PathVariable long helpDocumentId, @RequestParam boolean isHelpful) {
    helpManager.voteOnHelpDocument(helpDocumentId, isHelpful);
}
Run Code Online (Sandbox Code Playgroud)

执行时,它会抛出此错误:

org.springframework.web.bind.MissingServletRequestParameterException: Required boolean parameter 'isHelpful' is not present
Run Code Online (Sandbox Code Playgroud)

当然,isHelpful参数IS存在.我可以使上面的代码完美地用于HTTP POST,所以我知道这不是问题.

     $.ajax({
            url: "/help/" + helpDocumentId + "/vote.json",
            type: "PUT",
            data: {
                isHelpful: isHelpful
            },
            success: function(response) {
                // ....
            }
     });
Run Code Online (Sandbox Code Playgroud)

PUT是正确的http方法吗?此操作会修改helpDocument,但不会创建一个.

rest spring http spring-mvc http-put

19
推荐指数
2
解决办法
2万
查看次数

Spring MVC 415不支持的媒体类型

我使用Spring 3.2并尝试使用ajax post请求提交json对象数组.如果这是相关的,我逃脱了所有特殊字符.

我的HTTP状态为415.

我的控制器是:

@RequestMapping(value = "/save-profile", method = RequestMethod.POST,consumes="application/json")
    public @ResponseBody String saveProfileJson(@RequestBody String[] profileCheckedValues){
        System.out.println(profileCheckedValues.length);
        return "success";
    }
Run Code Online (Sandbox Code Playgroud)

jquery是:

jQuery("#save").click(function () {
        var profileCheckedValues = [];
        jQuery.each(jQuery(".jsonCheck:checked"), function () {
            profileCheckedValues.push($(this).val());
        });
        if (profileCheckedValues.length != 0) {
            jQuery("body").addClass("loading");
            jQuery.ajax({
                type: "POST",
                contentType: "application/json",
                url: contextPath + "/sample/save-profile",
                data: "profileCheckedValues="+escape(profileCheckedValues),
                dataType: 'json',
                timeout: 600000,
                success: function (data) {
                    jQuery('body').removeClass("loading");
                },
                error: function (e) {
                    console.log("ERROR: ", e);
                    jQuery('body').removeClass("loading");
                }
            });
        }
    });
Run Code Online (Sandbox Code Playgroud)

我发布的数组中的一个对象的示例是以下json:

{
  "id": "534213341",
  "name": "Jack …
Run Code Online (Sandbox Code Playgroud)

ajax jquery spring spring-mvc

14
推荐指数
2
解决办法
6万
查看次数

在Spring 3.2中使用AJAX的PUT方法不起作用

我正在尝试使用以下jQuery 1.6通过AJAX 在Spring(3.2.0)中调用一个方法.

function updateRoleEnabled(id)
{
    $.ajax({
            datatype:"json",                        
            type: "PUT",
            url: "/wagafashion/ajax/UpdateUserRole.htm",
            data: "id="+id+"&t="+new Date().getTime(),
            success: function(response)
            {
            },
            error: function(e)
            {
                alert('Error: ' + e);
            }
    });
}
Run Code Online (Sandbox Code Playgroud)

它试图在Spring中调用以下方法.

@RequestMapping(value=("ajax/UpdateUserRole"), method=RequestMethod.PUT)
public @ResponseBody void updateUserRole(@RequestParam(value=("id")) String id)
{
    System.out.println("id = "+id);
}
Run Code Online (Sandbox Code Playgroud)

FireFox响应以下错误.

HTTP状态405 - 不支持请求方法"GET"

类型状态报告

消息请求方法'GET'不受支持

description对于请求的资源,不允许使用指定的HTTP方法(不支持请求方法'GET').

Apache Tomcat/6.0.26

它适用于GETPOST方法和JSON(与Jackson-2.1.1)也适用于应用程序的其他部分.


如果需要查看dispatcher-servlet.xml文件,完整内容如下.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"

       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd …
Run Code Online (Sandbox Code Playgroud)

jquery spring json put jackson

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

标签 统计

spring ×4

jquery ×3

spring-mvc ×3

json ×2

ajax ×1

http ×1

http-put ×1

jackson ×1

java ×1

put ×1

rest ×1