java - 无法在 AJAX post 请求中自动绑定(bind)到 Spring MVC 中的 Java 对象

标签 java jquery ajax json spring-mvc

我有两个问题:

  1. 无法将我的参数自动绑定(bind)到 Java 对象中(它们正在被 使用 AJAX 通过 jQuery 发布)
  2. 我收到 406 错误,我认为这意味着我无法返回我想要的 JSON 对象

我相信这两个问题是不同的,但两者的解决方案都依赖于代码的相同部分。请问你能帮帮我吗?预先非常感谢!

Ajax 请求

我正在尝试发布一个非常简单的对象,有电子邮件和名称,如下所示。

var dataObject = {
    name : this.userName,
    email : this.userEmail,
};
this.$.ajax({
    type : "POST",
    url : '/stock-events/register-to-stock',
    contentType: "application/json",
    data : JSON.stringify(dataObject),
    success : function(data) {
        console.info("data");
    },
    dataType : 'json'
});

Spring MCV Controller

    @ResponseBody @RequestMapping(value = "/register-to-stock", method = RequestMethod.POST)
public StockAlarmJsonResponse submitForm(@Valid @RequestBody StockAlarmModel stockAlarm,
        BindingResult result, Model m) {
    System.out.println("hola");
    System.out.println(stockAlarm);
    StockAlarmJsonResponse response = new StockAlarmJsonResponse();
    if (result.hasErrors()) {
        System.out.println(result);
        response.fail();
    }else{
        System.out.println(stockAlarm);
        response.success();
    }
    return response;
}

型号

  public class StockAlarmModel {


@NotNull @Max(100) @Email
private String email;

@Max(100)
private String name;

@Override
public String toString() {
    return "Email: " + email + '\n' + "Name: " + name;
}
}

我在 Java 中遇到错误(看起来参数不存在)

 org.springframework.validation.BeanPropertyBindingResult: 1 errors
 Field error in object 'stockAlarmModel' on field 'email': rejected value [null]; codes   [NotNull.stockAlarmModel.email,NotNull.email,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [stockAlarmModel.email,email]; arguments []; default message [email]]; default message [may not be null]

我在 chrome bug 中遇到错误(看起来 json 对象没有成功返回)

  POST http://localhost:8080/stock-events/register-to-stock 400 (Bad Request) jquery-     1.9.1.js:8526
  send jquery-1.9.1.js:8526
  jQuery.extend.ajax jquery-1.9.1.js:7978
  StockAlarm.registerAlarm StockAlarm.js:131
  (anonymous function) StockAlarm.js:87
 jQuery.event.dispatch jquery-1.9.1.js:3074
elemData.handle

针对请求显示的 Chrome 错误

内容类型/应用程序 Json

请求负载:

{"name":"santiago","email":"santiago@tiendanube.com"}

最佳答案

我的印象是 jQuery 会序列化这个

var dataObject = {
    name : this.userName,
    email : this.userEmail,
};

转换成json,例如

{"name" : "some username", "email" : "some email"}

因此,您需要 Spring 将 JSON 对象绑定(bind)到您的命令对象。将您的方法更改为

public @ResponseBody StockAlarmJsonResponse submitForm(@Valid @RequestBody StockAlarmModel stockAlarm, BindingResult result, Model m) {

注意 @RequestBody (可以与@Valid一起使用)。 Spring 将使用 HttpMessageConverter 尝试将 JSON 反序列化为 StockAlarmModel 的实例。我不确定该对象是否会直接添加到 Model 中。如果需要,您可以随时自行添加。

据我所知ModelAttribute尝试将请求参数绑定(bind)到命令对象。您的 ajax 请求中没有请求参数。

关于java - 无法在 AJAX post 请求中自动绑定(bind)到 Spring MVC 中的 Java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18920031/

相关文章:

c# - 使用 Ajax 在 DIV 中显示列表

ajax - AngularJS 使用 $http.get 请求以特定顺序传递参数数据

javascript - 如何计算我创建的对象数量?

java - 如何处理DataIntegrityViolationException以获取具体原因?

javascript - 如何通过单击 A 标签更改 DIV 上的 CSS 值?

c# - 使用单一方法类——最好的方法?

javascript - 尝试使用 Javascript 和 jQuery 创建动态选择框

jquery - 通过ajax从cropit插件上传图像

java - 如何在Spring Cloud DataFlow中注册应用程序?

java - Spring Boot中跨子域共享cookie