Java spring mvc-带post的空json
我对SpringMVC有一个小问题。基本上,我要做的是使用AJAX将JSON对象发送到服务器 简而言之,JS代码如下:Java spring mvc-带post的空json,java,jquery,ajax,spring-mvc,Java,Jquery,Ajax,Spring Mvc,我对SpringMVC有一个小问题。基本上,我要做的是使用AJAX将JSON对象发送到服务器 简而言之,JS代码如下: var newUser = { "userId":-1, "userName":name }; var notifRecip = { "emailNotification":jQuery( this ).find( "td:nth-child(3) input[type='checkbox']" ).is( ":checked" ), "smsNotification"
var newUser = { "userId":-1, "userName":name };
var notifRecip = {
"emailNotification":jQuery( this ).find( "td:nth-child(3) input[type='checkbox']" ).is( ":checked" ),
"smsNotification":jQuery( this ).find( "td:nth-child(4) input[type='checkbox']" ).is( ":checked" ),
"webNotification":jQuery( this ).find( "td:nth-child(5) input[type='checkbox']" ).is( ":checked" ),
"user":newUser
};
如果我是对的,它会给我一个有效的JSON对象,其中包含来自表单的数据。是吗?现在我在做ajax帖子:
jQuery.ajax( "/notifications/saveNewUsers/" + notId, {
dataType:'json',
contentType:"application/json",
type:"POST",
data:( notifRecip )
} )
.success( function ( response )
{
console.log( response )
} );
到目前为止一切正常,我的控制器接收到带有正确notId
值的请求:
@RequestMapping(value = "saveNewUsers/{notificationId}", method = RequestMethod.POST, headers = {"content-type=application/json"})
public
@ResponseBody
boolean saveNewUsers( @RequestBody NotificationRecipient notificationRecipient, @PathVariable Integer notificationId )
{
System.out.println( notificationId + " " + notificationRecipient );
return false;
}
但控制台输出如下所示:
18:45:10,947 INFO [stdout] (ajp--127.0.0.1-8009-1) 0 null
我不知道为什么。我是spring mvc的新手,因此任何帮助都将不胜感激。我确实有Jackson
依赖关系
NotificationRecipient
对象:
public class NotificationRecipient
{
private User user;
private boolean emailNotification;
private boolean smsNotification;
private boolean webNotification;
//with getters and setters
}
和用户
:
public class User
{
private Integer userId;
private String userName;
//with getters and setters
}
在谷歌搜索了一段时间后:
您需要调用
data:JSON.stringify(notifRecip)
。当我在没有它的情况下发布时,JSON对象中的参数被解析为GET参数(…webnotification=false&smsNotification=false…)您可以发布JSON,然后发送给控制器吗?(请参阅firebug中的并发布)