在Spring MVC中获取JSON表单的绑定错误的详细信息 [英] Getting details of binding errors for a JSON form in Spring MVC

查看:56
本文介绍了在Spring MVC中获取JSON表单的绑定错误的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring MVC 3.2.4.我有一个控制器方法,该方法从HTML表单接收数据.该方法具有以下签名:

I use Spring MVC 3.2.4. I have a controller method that receives data from an HTML form. The method has the following signature:

@RequestMapping(...)
public String updateProduct(@Valid Product product, BindingResult bindingResult)

在该方法中,我可以获得有关数据绑定(例如,整数字段中的非整数值)和 validation (JSR-303)错误的信息通过以下方式:

In the method, I can get information about both data binding (e.g., a non-integer value in an integer field) and validation (JSR-303) errors in the following way:

if (bindingResult.hasErrors()) {
    List<FieldError> errors = bindingResult.getFieldErrors();

    for (FieldError error : errors) {
        System.out.println(error.getObjectName() + " - " + error.getCode());
    }
}

我想更改此方法,以便它可以接收JSON格式而不是application/x-www-form-urlencoded的数据.我在方法签名中添加了@RequestBody批注:

I want to change this method so that it could receive data in JSON format instead of application/x-www-form-urlencoded. I added @RequestBody annotation to the method signature:

@RequestMapping(...)
public String updateProduct(@Valid @RequestBody Product product, BindingResult bindingResult)

在更新的方法中,我仍然可以以相同的方式获取所有 validation 错误.但是,数据绑定错误会生成HttpMessageNotReadableException.异常似乎没有用于访问错误代码和字段/对象名称之类的属性.此外,该异常会阻止JSR-303验证,并且直到下一次提交数据时,用户才会收到任何JSR-303验证消息.

In the updated method I still can get all validation errors in the same way. However, data binding errors generate an HttpMessageNotReadableException. The exception seems to have no properties for accessing things like the error code and field/object name. Besides, the exception prevents the JSR-303 validation, and the user does not receive any JSR-303 validation messages till the next data submission.

当我实现接收JSON的控制器方法时,如何获取有关数据绑定错误的信息?

How can I get the information about data binding errors when I implement a controller method that receives JSON?

更新:

我可以想到的一种解决方法是将Product类中的所有字段更改为String,然后使用JSR-303进行验证.这样,就不会发生数据绑定错误.但是我将不得不使用适当的数据类型创建Product类的克隆.

One workaround that I can think of is changing all fields in the Product class to String and then validating them with JSR-303. This way, no data binding errors will ever happen. But I will have to create a clone of the Product class with proper data types.

推荐答案

不要困惑, 无法读取 HttpInputMessage .使用JSON,这可能意味着 MappingJackson2HttpMessageConverter .如果在将JSON反序列化为您指定的任何类型的实例(在本例中为Product)的实例时出错,它将引发该异常.如果JSON格式错误,则可能会发生这种情况.

Don't get confused, HttpMessageNotReadableException has nothing to do with validation errors. It gets thrown when an HttpMessageConverter cannot read an HttpInputMessage. With JSON, that probably means MappingJackson2HttpMessageConverter. It will throw that exception if something goes wrong while deserializing the JSON into an instance of whatever type you specified, Product in this case. This can happen if the JSON is malformed.

您无法在BindingResult中捕获这些错误.

You cannot catch those errors in a BindingResult.

这篇关于在Spring MVC中获取JSON表单的绑定错误的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆