weixin_33695082 2012-06-25 16:59 采纳率: 0%
浏览 23

Spring MVC-带发布的null json

I have small problem with Spring MVC. Basically what I'm trying to do is send JSON object to server with AJAX.

In short it looks like this, the JS code:

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
};

If I'm right, it gives me a valid JSON object with data from my form yes? Now I'm doing ajax post:

jQuery.ajax( "/notifications/saveNewUsers/" + notId, {
       dataType:'json',
      contentType:"application/json",
     type:"POST",
    data:( notifRecip )
} )
.success( function ( response )
{
    console.log( response )
} );

So far so good, my controller recives request with proper notId value:

@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;
}

But console output is like this:

18:45:10,947 INFO  [stdout] (ajp--127.0.0.1-8009-1) 0 null

And I dont know why. I'm quite new to spring mvc so any help would be appreciated. I do have Jackson dependency.

the NotificationRecipient object:

public class NotificationRecipient
{
private User user; 
private boolean emailNotification;
private boolean smsNotification;
private boolean webNotification;

//with getters and setters
}

and User:

public class User
{
private Integer userId;
private String userName;

//with getters and setters
}
  • 写回答

1条回答 默认 最新

  • 衫裤跑路 2012-06-25 20:41
    关注

    After quite a while of googling:

    You need to call data: JSON.stringify( notifRecip ). When I did POST without it, parameters from my JSON object were parsed as GET parameters ( ... webNotificaiton=false&smsNotification=false...)

    评论

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用