如何从 BindingResult 获取控制器中的错误文本?
我有一个返回 JSON 的控制器。它采用一种形式,通过 spring 注释来验证自身。我可以从 BindingResult
获取 FieldError
列表,但它们不包含 JSP 将在
中显示的文本标签。如何获取以 JSON 格式发回的错误文本?
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody JSONResponse submit(@Valid AnswerForm answerForm, BindingResult result, Model model, HttpServletRequest request, HttpServletResponse response) {
if (result.hasErrors()) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
JSONResponse r = new JSONResponse();
r.setStatus(JSONResponseStatus.ERROR);
//HOW DO I GET ERROR MESSAGES OUT OF BindingResult???
} else {
JSONResponse r = new JSONResponse();
r.setStatus(JSONResponseStatus.OK);
return r;
}
}
JSONREsponse 类只是一个 POJO
public class JSONResponse implements Serializable {
private JSONResponseStatus status;
private String error;
private Map<String,String> errors;
private Map<String,Object> data;
// ...getters and setters...
}
,调用 BindingResult.getAllErrors()
返回一个 FieldError
对象数组,但它没有实际的错误。
I have an controller that returns JSON. It takes a form, which validates itself via spring annotations. I can get FieldError
list from BindingResult
, but they don't contain the text that a JSP would display in the <form:errors>
tag. How can I get the error text to send back in JSON?
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody JSONResponse submit(@Valid AnswerForm answerForm, BindingResult result, Model model, HttpServletRequest request, HttpServletResponse response) {
if (result.hasErrors()) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
JSONResponse r = new JSONResponse();
r.setStatus(JSONResponseStatus.ERROR);
//HOW DO I GET ERROR MESSAGES OUT OF BindingResult???
} else {
JSONResponse r = new JSONResponse();
r.setStatus(JSONResponseStatus.OK);
return r;
}
}
JSONREsponse class is just a POJO
public class JSONResponse implements Serializable {
private JSONResponseStatus status;
private String error;
private Map<String,String> errors;
private Map<String,Object> data;
// ...getters and setters...
}
Calling BindingResult.getAllErrors()
returns an array of FieldError
objects, but it does not have the actual errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
免责声明:我仍然不使用 Spring-MVC 3.0
但我认为 Spring 2.5 使用的相同方法可以满足您的需求
我希望它对您有用
更新
如果您想获得由你的资源包,你需要一个注册的messageSource实例(它必须被称为messageSource)
在你的视图中注入你的MessageSource实例
并获取你的消息,如下所示
你的验证器应该看起来像
Disclaimer: I still do not use Spring-MVC 3.0
But i think the same approach used by Spring 2.5 can fullfil your needs
I hope it can be useful to you
UPDATE
If you want to get the message provided by your resource bundle, you need a registered messageSource instance (It must be called messageSource)
Inject your MessageSource instance inside your View
And to get your message, do as follows
Your Validator should looks like
我最近遇到了这个问题,找到了一个更简单的方法(也许是Spring 3的支持)
不需要更改/添加消息源。
I met this problem recently, and found an easier way (maybe it's the support of Spring 3)
There's no need to change/add the message source.
使用 Java 8 流
With Java 8 Streams
BEAN XML:
JAVA:
注意: 调用 Errors.getDefaultMessage() 不一定会返回从代码 + 参数生成的相同消息。 defaultMessage 是调用 Errors.rejectValue() 方法时定义的单独值。请参阅Errors.rejectValue() API 此处
BEAN XML:
JAVA:
NOTE: Calling Errors.getDefaultMessage() will not necessarily return the same message that is generated from the code + args. The defaultMessage is a separate value defined when calling the Errors.rejectValue() method. See Errors.rejectValue() API Here
WebMvcConfigurerAdapter:
控制器:
WebMvcConfigurerAdapter:
Controller:
获取与每个字段关联的所有错误的
Map
:获取与每个字段关联的错误合并在一起作为单个
String
的Map
>:To obtain a
Map
of all the errors associated with each field:To obtain a
Map
with the errors associated with each field merged together as a singleString
: