# 35. 验证

只要在类路径上存在JSR-303实现（比如Hibernate validator），Bean Validation 1.1支持的方法验证特性就会自动启用。这允许bean方法的参数和/或者返回值，被标注为`javax.validation`约束。为了让这样的带标注的方法在搜索行内约束标注时被找到，拥有它们的目标类，需要在类型层次上被标注为`@Validated`。

例如，下面的服务触发第一个参数的验证，确保它的大小在8和10之间：

```java
@Service
@Validated
public class MyBean {

    public Archive findByCodeAndAuthor(@Size(min = 8, max = 10) String code,
            Author author) {
        ...
    }

}
```


---

# 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://jack80342.gitbook.io/spring-boot/iv.-spring-boot-features/35.-validation.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.
