我对 Spring MVC 有一个小问题。基本上我要做的是使用 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":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
}
和User
:
public class User
{
private Integer userId;
private String userName;
//with getters and setters
}