# 3.2. 将代码解析为错误消息

我们涵盖了数据绑定和验证。本节介绍与验证错误相对应的输出消息。在上[一节](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#validator)所示的示例中，我们拒绝了`name`和`age`字段。如果我们想通过使用`MessageSource`来输出错误消息 ，我们可以使用我们在拒绝字段时提供的错误代码（在本例中为“姓名”和“年龄”）来完成。当您调用（直接或间接，通过使用例如`ValidationUtils`类）`rejectValue`或`Errors`接口中的其他`reject`方法之一时，底层实现不仅注册您传入的代码，而且还注册许多额外的错误代码。`MessageCodesResolver` 确定`Errors`接口寄存器的错误代码。默认情况下， 使用`DefaultMessageCodesResolver`，它（例如）不仅使用您提供的代码注册消息，而且还注册包含您传递给拒绝方法的字段名称的消息。所以，如果你用`rejectValue("age", "too.darn.old")` 拒绝一个字段，除了代码`too.darn.old`之外，Spring 还会注册`too.darn.old.age`和 `too.darn.old.age.int`（第一个包含字段名称，第二个包含字段类型）。这样做是为了方便开发人员定位错误消息。

有关`MessageCodesResolver`和 默认策略的更多信息可以分别在[`MessageCodesResolver`](https://docs.spring.io/spring-framework/docs/5.3.22/javadoc-api/org/springframework/validation/MessageCodesResolver.html)和 [`DefaultMessageCodesResolver`](https://docs.spring.io/spring-framework/docs/5.3.22/javadoc-api/org/springframework/validation/DefaultMessageCodesResolver.html)的 javadoc 中找到 。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.shiker.tech/spring-he-xin-gong-neng/3.-yan-zheng-shu-ju-bang-ding-he-lei-xing-zhuan-huan/3.2.-jiang-dai-ma-jie-xi-wei-cuo-wu-xiao-xi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
