Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Springbean验证与hibernate注释警告消息解析_Java_Validation_Spring Mvc_Bean Validation - Fatal编程技术网

Java Springbean验证与hibernate注释警告消息解析

Java Springbean验证与hibernate注释警告消息解析,java,validation,spring-mvc,bean-validation,Java,Validation,Spring Mvc,Bean Validation,我有一个问题,就是如何在Spring表单中使用hibernate注释来解析消息。 我在控制器中有以下方法: @RequestMapping(value = "/path", method = RequestMethod.POST) public CustResponse storeForm(HttpServletResponse response, HttpServletRequest request, @Valid @RequestBody Form for

我有一个问题,就是如何在Spring表单中使用hibernate注释来解析消息。 我在控制器中有以下方法:

 @RequestMapping(value = "/path", method = RequestMethod.POST)
    public  CustResponse storeForm(HttpServletResponse response, HttpServletRequest request,
            @Valid @RequestBody Form form, BindingResult result) {
当请求到来时,我希望将所有错误消息解析为CustResponse对象。为了解析警告消息,我使用BindingResult中的以下方法:

result.getCode();
根据文件,它:

  • 返回此可解析文件的默认代码,即*last 代码数组中的一个
默认代码的解析非常一般:对于此注释,不为空:

import org.hibernate.validator.constraints.NotBlank;
但是,BindingResult中还存在其他更具体的错误代码。例如: [NotBlank.form.fieldNAme,NotBlank.fieldNAme,NotBlank.java.lang.String,NotBlank]

我认为使用最有意义的错误代码来解决错误消息更有意义: NotBlank.form.fieldNAme 为了解决警告消息,我不想遍历所有错误代码。
您建议如何处理此类问题?

我想知道您是如何解决代码的。还是按原样返回呢?我使用的是这个API:当然,但这会得到代码,而不是消息。消息的意思是“字段名不能为空”。还是在JSP上使用
spring:message
标记?我理解你的问题,但我不明白为什么你首先需要一个代码,谁会用它做什么。这使得我很难提出一个解决方案。我正在使用定制的资源包管理器来获取基于代码的真实消息,它工作正常。我不使用jsp标记,因为我需要返回带有翻译错误消息的自定义json。主要问题是密钥被解析为NotBlank,并且我无法获得正确的错误消息。“最有意义”的问题在于它取决于具体情况。恐怕您需要重复一下。在我的项目中,我决定总是在注释中添加一条消息,比如i18n的
@NotBlank(message=“Name不得为空”)
@NotBlank(message=“NotBlank.form.fieldName”)
。这样,我总能得到一个完美的信息,没有任何麻烦。