{"id":290744,"date":"2018-10-14T10:30:02","date_gmt":"2018-10-14T06:30:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=290744"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2018-10-14T06:30:02","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=290744","title":{"rendered":"\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u2014 Java &#038; Spring Validation"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u0430 (bean) \u0432 java \u0442\u0435\u043c\u0430 \u043d\u0435 \u043d\u043e\u0432\u0430\u044f, \u043d\u043e \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u0430\u044f \u0438 \u0437\u0434\u0435\u0441\u044c \u044f \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044e \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0430\u0441\u043f\u0435\u043a\u0442\u044b: \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 JSR-303, \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0438\u0441\u0442\u043e \u0432 Java \u0438 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Spring, \u043a\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0438 \u0432 Web.<br \/>  \u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435: \u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 (JSR-303) \u0432<br \/>   \u2014 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u043c Java \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438<br \/>   \u2014 c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Spring<br \/>   \u2014 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 Java + Spring<br \/>   \u2014 Spring MVC <br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h3>Validation \u0432 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u043c Java \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438<\/h3>\n<p>  \u0414\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u043d\u0430 \u043f\u043e\u043b\u044f\u0445 \u043a\u043b\u0430\u0441\u0441\u0430, \u0442.\u0435. \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c. \u0410\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u0435\u0441\u0442\u044c \u0443\u0436\u0435 \u0433\u043e\u0442\u043e\u0432\u044b\u0435: <\/p>\n<blockquote><p><a href=\"https:\/\/habr.com\/users\/null\/\" class=\"user_link\">Null<\/a>, @DecimalMin, @Digits, <a href=\"https:\/\/habr.com\/users\/pattern\/\" class=\"user_link\">Pattern<\/a>, <a href=\"https:\/\/habr.com\/users\/email\/\" class=\"user_link\">Email<\/a> \u0438 \u0434\u0440.<\/p><\/blockquote>\n<p>, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435. \u0418 \u0442\u0430\u043a \u0435\u0441\u0442\u044c \u043a\u043b\u0430\u0441\u0441 (bean)   <\/p>\n<pre><code class=\"java\">import javax.validation.constraints.Digits; import javax.validation.constraints.Size;  public class Person {      @Size(min=2, max=50)     private String Name;      @Digits(integer=3, fraction=0, message = \"\u041d\u0435 \u0431\u043e\u043b\u0435\u0435 3-\u0445 \u0437\u043d\u0430\u043a\u043e\u0432\")     @PersonAgeConstraint     private Integer age;      public Person(String name, Integer age) {         Name = name;         this.age = age;     } } <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 <a href=\"https:\/\/habr.com\/users\/size\/\" class=\"user_link\">Size<\/a> \u0438 @Digits \u0433\u043e\u0442\u043e\u0432\u044b\u0435 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438, \u0430 @PersonAgeConstraint \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f. \u041a\u0430\u043a \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e:<br \/>   \u2014 \u043f\u043e\u0434\u0433\u043e\u0442\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e   <\/p>\n<pre><code class=\"java\">@Target({ElementType.METHOD, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy=PersonAgeConstraintValidator.class) public @interface PersonAgeConstraint {     String message() default \"{value.negative}\";     Class&lt;?&gt;[] groups() default {};     Class&lt;? extends Payload&gt;[] payload() default {}; } <\/code><\/pre>\n<p>  \u0412 message() \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043a\u043b\u044e\u0447 (value.negative) \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 \u0440\u0435\u0441\u0443\u0440\u0441\u0430 (ValidationMessages.properties) \u0434\u043b\u044f \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f  <\/p>\n<blockquote><p>value.negative=\u041e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435\\u0020\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/p><\/blockquote>\n<p>   \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043a\u043b\u0430\u0441\u0441\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u2014 PersonAgeConstraintValidator.class  <\/p>\n<pre><code class=\"java\">public class PersonAgeConstraintValidator implements ConstraintValidator&lt;PersonAgeConstraint, Integer&gt; {     @Override     public boolean isValid(Integer age, ConstraintValidatorContext constraintValidatorContext) {         return age &gt; 0;     } } <\/code><\/pre>\n<p>  \u041c\u043e\u044f \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f \u0433\u043e\u0442\u043e\u0432\u0430, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0435 \u043a \u043f\u043e\u043b\u044e \u0438 \u0443\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0432\u0441\u0435 \u043f\u043e\u043b\u044f \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0435\u0441\u0442\u044c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u0435\u043d\u044b \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c\u0438.  <\/p>\n<pre><code class=\"java\">import javax.validation.Validator; \/**  * Test Validation  *\/ public class DemoJValidationApplicationTests {      \/\/ \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f Validator     private static Validator validator;     static {         ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();         validator = validatorFactory.usingContext().getValidator();     }      @Test     public void testValidators() {         final Person person = new Person(\"\u0418\u0432\u0430\u043d \u041f\u0435\u0442\u0440\u043e\u0432\", -4500);          Set&lt;ConstraintViolation&lt;Person&gt;&gt; validates = validator.validate(person);         Assert.assertTrue(validates.size() &gt; 0);         validates.stream().map(v -&gt; v.getMessage())                 .forEach(System.out::println);     } } <\/code><\/pre>\n<p>  \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 <br \/>  <code><i>\u041d\u0435 \u0431\u043e\u043b\u0435\u0435 3-\u0445 \u0437\u043d\u0430\u043a\u043e\u0432<br \/>  \u041e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<br \/>  <\/i><\/code> <br \/>  \u0421\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0439 \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0432 \u0444\u0430\u0439\u043b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439, \u043f\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u0443:<br \/>  <code>AnnotationName.entity.fieldname=\u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 <\/code><br \/>  \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/bo\/m8\/kb\/bom8kbzpz9pfflsoxwr-grxcchk.jpeg\" alt=\"image\">  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">pom \u0444\u0430\u0439\u043b<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt; &lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" \txsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt; \t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;  \t&lt;groupId&gt;com.example&lt;\/groupId&gt; \t&lt;artifactId&gt;DemoJSRvalidation&lt;\/artifactId&gt; \t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt; \t&lt;packaging&gt;jar&lt;\/packaging&gt;  \t&lt;name&gt;DemoJSRvalidation&lt;\/name&gt; \t&lt;description&gt;Demo project for Spring Boot JSR-303 validation&lt;\/description&gt;  \t&lt;parent&gt; \t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t&lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt; \t\t&lt;version&gt;2.0.5.RELEASE&lt;\/version&gt; \t\t&lt;relativePath\/&gt; &lt;!-- lookup parent from repository --&gt; \t&lt;\/parent&gt;  \t&lt;properties&gt; \t\t&lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt; \t\t&lt;project.reporting.outputEncoding&gt;UTF-8&lt;\/project.reporting.outputEncoding&gt; \t\t&lt;java.version&gt;1.8&lt;\/java.version&gt; \t&lt;\/properties&gt;  \t&lt;dependencies&gt; \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-boot-starter-validation&lt;\/artifactId&gt; \t\t&lt;\/dependency&gt;  \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-boot-starter-test&lt;\/artifactId&gt; \t\t\t&lt;scope&gt;test&lt;\/scope&gt; \t\t&lt;\/dependency&gt; \t&lt;\/dependencies&gt;  \t&lt;build&gt; \t\t&lt;plugins&gt; \t\t\t&lt;plugin&gt; \t\t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t\t&lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt; \t\t\t&lt;\/plugin&gt; \t\t&lt;\/plugins&gt; \t&lt;\/build&gt;   &lt;\/project&gt;  <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <b>\u041f\u043e\u044d\u0442\u0430\u043f\u043d\u0430\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430.<\/b> \u0414\u043b\u044f Class&lt;?&gt;[] groups() \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0442\u0438\u043f\u044b \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0442\u043e\u043c \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u043a, \u0442.\u0435. \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0444\u0438\u043b\u044c\u0442\u0440. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e \u044d\u0442\u0430\u043f\u0430\u043c, 1) \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043b\u0438\u0446\u0430 \u043f\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e \u0437\u0434\u043e\u0440\u043e\u0432\u044c\u044f, 2) \u0430 \u0443\u0436\u0435 \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u043e\u0444\u0435\u0441\u0441\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435. \u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u043c \u0434\u0432\u0435 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 <br \/>  HealthConstraint \u0438 ProfessionalConstraint \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u043d\u0438\u0445. \u041f\u0435\u0440\u0432\u044b\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u0437\u0434\u043e\u0440\u043e\u0432\u044c\u044e \u0430 \u0437\u0430\u0442\u0435\u043c \u0435\u0441\u043b\u0438 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u043f\u043e \u0437\u0434\u043e\u0440\u043e\u0432\u044c\u044e, \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u043d\u0430 \u043f\u0440\u043e\u0444\u0435\u0441\u0441\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435. <br \/>   \u043f\u0440\u0438\u043c\u0435\u0440:  <\/p>\n<pre><code class=\"java\">public class Person {      @HealthConstraint(groups = Health.class)     private Documents healthDocuments;      @ProfessionalConstraint(groups = Professional.class)     private Documents ProfessionalDocuments;     \/\/... } <\/code><\/pre>\n<p>  \u043f\u0440\u0438\u043c\u0435\u0440 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 HealthConstraint   <\/p>\n<pre><code class=\"java\">@Target({ElementType.METHOD, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy=HealthConstraintValidator.class) public @interface HealthConstraint {     String message() default \"{health.documents}\";     Class&lt;?&gt;[] groups() default {};     Class&lt;? extends Payload&gt;[] payload() default {}; } <\/code><\/pre>\n<p>  \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 HealthConstraintValidator   <\/p>\n<pre><code class=\"java\">public class HealthConstraintValidator implements ConstraintValidator&lt;HealthConstraint, Documents&gt; {     @Override     public boolean isValid(Documents documents, ConstraintValidatorContext constraintValidatorContext) {         return documents.contains(\"\u0441\u043f\u0440\u0430\u0432\u043a\u0430 1\");     } } <\/code><\/pre>\n<p>  \u0434\u043b\u044f ProfessionalConstraint \u0432\u0441\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e<\/p>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"java\">    @Test     public void healthAndProfessionalValidators() {         final Person person = new Person(\"\u0418\u0432\u0430\u043d \u041f\u0435\u0442\u0440\u043e\u0432\", 45);         person.setHealthDocuments(new Documents(Arrays.asList(\"\u0441\u043f\u0440\u0430\u0432\u043a\u0430 1\", \"\u0441\u043f\u0440\u0430\u0432\u043a\u0430 3\")));         person.setProfessionalDocuments(new Documents(Arrays.asList(\"\u0442\u0435\u0441\u0442 1\", \"\u0442\u0435\u0441\u0442 4\")));          \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u0437\u0434\u043e\u0440\u043e\u0432\u044c\u0435         Set&lt;ConstraintViolation&lt;Person&gt;&gt; validates = validator.validate(person, Health.class);         Assert.assertTrue(validates.size() == 0);                  \/\/ \u0438 \u0435\u0441\u043b\u0438 \u0437\u0434\u043e\u0440\u043e\u0432\u044c\u0435 \u041e\u043a, \u0442\u043e \u043f\u0440\u043e\u0444. \u0442\u0435\u0441\u0442         validates = validator.validate(person, Professional.class);         Assert.assertTrue(validates.size() == 0);                      }  <\/code><\/pre>\n<p>  \u041f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043d\u0443\u0436\u043d\u044b \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430, web service \u0438 \u0434\u0440. \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432.   <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u043a\u043b\u0430\u0441\u0441 Documents<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"java\">public class Documents {      private List&lt;String&gt; tests = new ArrayList();      public Documents(List&lt;String&gt; tests) {         this.tests.addAll(tests);     }      public boolean contains(String test) {         return this.tests.contains(test);     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<h3>Validation c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Spring<\/h3>\n<p>  \u0412 Spring \u0435\u0441\u0442\u044c \u0442\u0430\u043a \u0436\u0435 \u0441\u0432\u043e\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 Validator <\/p>\n<blockquote><p>(org.springframework.validation.Validator)<\/p><\/blockquote>\n<p> \u043a\u0430\u043a \u0438 \u0432 Java <\/p>\n<blockquote><p>(javax.validation.Validator)<\/p><\/blockquote>\n<p> \u0438 \u0438\u043c\u0435\u043d\u043d\u043e \u0435\u0433\u043e \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0434\u0430\u043d\u043d\u044b\u0445. \u042d\u0442\u043e \u0443\u0436\u0435 \u043d\u0435 \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434, \u043d\u043e \u0432 \u043d\u0435\u043c \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u044f \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u044c \u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u043c\u043e\u0441\u0442\u044c. \u0414\u043b\u044f \u0442\u043e\u0433\u043e \u0436\u0435 \u0431\u0438\u043d\u0430, \u0441\u0434\u0435\u043b\u0430\u044e \u0442\u0443\u0436\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430.<br \/>  \u041f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0432 \u0434\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430, \u0434\u0435\u043b\u0430\u0435\u043c \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e  <\/p>\n<pre><code class=\"java\">@Service public class PersonValidator implements Validator {     @Override     public boolean supports(Class&lt;?&gt; aClass) {         return Person.class.equals(aClass);     }      @Override     public void validate(Object obj, Errors errors) {         Person p = (Person) obj;         if (p.getAge() &lt; 0) {             errors.rejectValue(\"age\", \"value.negative\");         }     } } <\/code><\/pre>\n<p>  value.negative \u2014 \u0442\u0430\u043a \u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043b\u044e\u0447\u043e\u043c \u0432 \u0444\u0430\u0439\u043b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439, <code>public boolean supports<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0442\u0438\u043f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430. <br \/>  \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 DataBinder<br \/>  \u041f\u0440\u0438\u043c\u0435\u0440:  <\/p>\n<pre><code class=\"java\">@RunWith(SpringRunner.class) @SpringBootTest public class DemoJValidationApplicationTests { \/\/ \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0444\u0430\u0439\u043b \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439     private static final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();     static {         messageSource.setBasename(\"message\");     }         @Autowired        private PersonValidator personValidator;  \t@Test \tpublic void testValidators() { \t\tfinal Person person = new Person(\"\u0418\u0432\u0430\u043d \u041f\u0435\u0442\u0440\u043e\u0432\", -4500);  \t\tfinal DataBinder dataBinder = new DataBinder(person); \t\tdataBinder.addValidators(personValidator); \t\tdataBinder.validate();                  Assert.assertTrue(dataBinder.getBindingResult().hasErrors());  \t\tif (dataBinder.getBindingResult().hasErrors()) { \t\t\tdataBinder.getBindingResult().getAllErrors().stream(). \t\t\t\t\tforEach(e -&gt; System.out.println(messageSource \t\t\t\t\t\t\t.getMessage(e, Locale.getDefault()))); \t\t} \t} } <\/code><\/pre>\n<p>  \u0411\u0443\u0434\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u044b \u0432\u0441\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043b\u0438 org.springframework.validation.Validator \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430 Person<br \/>  \u041c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0430\u043a \u0436\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u043e\u0432, dataBinder.addValidators, \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043f\u0440\u0430\u0432\u0438\u043b (\u0432\u044b\u0437\u043e\u0432 \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u0430, \u0434\u0440\u0443\u0433\u043e\u0433\u043e), \u043f\u0440\u0438\u043c\u0435\u0440:  <\/p>\n<pre><code class=\"java\">public class OtherValidator implements Validator {      @Override     public boolean supports(Class&lt;?&gt; aClass) {         return Person.class.equals(aClass);     }      @Override     public void validate(Object obj, Errors errors) {         \/\/ ...     } } \/\/--------- @Service public class PersonValidator implements Validator {      \/**      *  \u0434\u0440\u0443\u0433\u043e\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430      *\/         @Autowired     private OtherValidator otherValidator;      @Override     public void validate(Object obj, Errors errors) {         Person p = (Person) obj;         if (p.getAge() &lt; 0) {             errors.rejectValue(\"age\", \"value.negative\");         }         \/\/ \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u0430, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0434\u0440\u0443\u0433\u043e\u0435         otherValidator.validate(obj, errors);     } } <\/code><\/pre>\n<p>  \u042f \u043f\u043e\u0447\u0435\u043c\u0443 \u0442\u043e \u043e\u0436\u0438\u0434\u0430\u043b, Spring \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0435 \u0432 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f\u0445, \u043d\u043e \u043d\u0435\u0442, \u044d\u0442\u043e\u0442 \u0432\u044b\u0437\u043e\u0432 \u043d\u0430\u0434\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e.<br \/>  \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/sf\/dz\/vn\/sfdzvngixyigi211nfqxui8lzt8.png\" alt=\"image\">  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">pom \u0444\u0430\u0439\u043b<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt; &lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" \txsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt; \t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;  \t&lt;groupId&gt;com.example&lt;\/groupId&gt; \t&lt;artifactId&gt;DemoJSRvalidation&lt;\/artifactId&gt; \t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt; \t&lt;packaging&gt;jar&lt;\/packaging&gt;  \t&lt;name&gt;DemoJSRvalidation&lt;\/name&gt; \t&lt;description&gt;Demo project for Spring Boot JSR-303 validation&lt;\/description&gt;  \t&lt;parent&gt; \t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t&lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt; \t\t&lt;version&gt;2.0.5.RELEASE&lt;\/version&gt; \t\t&lt;relativePath\/&gt; &lt;!-- lookup parent from repository --&gt; \t&lt;\/parent&gt;  \t&lt;properties&gt; \t\t&lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt; \t\t&lt;project.reporting.outputEncoding&gt;UTF-8&lt;\/project.reporting.outputEncoding&gt; \t\t&lt;java.version&gt;1.8&lt;\/java.version&gt; \t&lt;\/properties&gt;  \t&lt;dependencies&gt; \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-boot-starter-validation&lt;\/artifactId&gt; \t\t&lt;\/dependency&gt;  \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-boot-starter-test&lt;\/artifactId&gt; \t\t\t&lt;scope&gt;test&lt;\/scope&gt; \t\t&lt;\/dependency&gt; \t&lt;\/dependencies&gt;  \t&lt;build&gt; \t\t&lt;plugins&gt; \t\t\t&lt;plugin&gt; \t\t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t\t&lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt; \t\t\t&lt;\/plugin&gt; \t\t&lt;\/plugins&gt; \t&lt;\/build&gt;   &lt;\/project&gt; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<h3>Java &amp; Spring<\/h3>\n<p>  \u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u044f \u0437\u0430\u0445\u043e\u0447\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0430 \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0432 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u2014 Java \u0438 Spring, \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0438\u0445 \u043c\u043e\u0436\u043d\u043e, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 Spring validator \u0432\u044b\u0437\u043e\u0432 <i>javax.validation.Validator<\/i> <br \/>  \u041f\u0440\u0438\u043c\u0435\u0440  <\/p>\n<pre><code class=\"java\">import javax.validation.Validator;  @Service public class PersonValidator implements org.springframework.validation.Validator {      \/\/ javax.validation.Validator     @Autowired     private Validator validator;      @Override     public boolean supports(Class&lt;?&gt; aClass) {         return Person.class.equals(aClass);     }      @Override     public void validate(Object obj, Errors errors) {         Set&lt;ConstraintViolation&lt;Object&gt;&gt; validates = validator.validate(obj);          for (ConstraintViolation&lt;Object&gt; constraintViolation : validates) {             String propertyPath = constraintViolation.getPropertyPath().toString();             String message = constraintViolation.getMessage();             errors.rejectValue(propertyPath, \"\", message);         }          Person p = (Person) obj;         if (p.getAge() &lt; 0) {             errors.rejectValue(\"age\", \"only.positive.numbers\");         }     } } <\/code><\/pre>\n<p>  \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e spring \u0434\u0435\u043b\u0430\u0435\u043c injection <code>javax.validation.Validator<\/code><br \/>  <code>@Autowired<br \/>   private Validator validator; <br \/>  <\/code>\u0434\u0430\u043b\u0435\u0435 \u043d\u0430 \u043c\u0435\u0442\u043e\u0434\u0435 <code>public void validate(Object obj, Errors errors)<\/code> <br \/>  \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 java, \u0430 \u0437\u0430\u0442\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0432\u0441\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430 Person \u043d\u0430 spring org.springframework.validation.Validator.<br \/>  \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0442\u0430\u043a\u0436\u0435 \u0447\u0435\u0440\u0435\u0437 spring  <\/p>\n<pre><code class=\"java\">    @Test     public void testValidators() {         final Person person = new Person(\"\u0418\u0432\u0430\u043d\", -4500);          final DataBinder dataBinder = new DataBinder(person);         dataBinder.addValidators(personValidator);         dataBinder.validate();          if (dataBinder.getBindingResult().hasErrors()) {            dataBinder.getBindingResult().getAllErrors()          \/\/ .... <\/code><\/pre>\n<p>   \u0442\u0435\u043f\u0435\u0440\u044c \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043e\u0442 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0439 java \u0438 spring (org.springframework.validation.Validator) \u0434\u043b\u044f Person <br \/>  \u0412\u044b\u0432\u043e\u0434 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438<br \/>  <code>\u041e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <i>(\u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f)<\/i><br \/>  \u041d\u0435 \u0431\u043e\u043b\u0435\u0435 3-\u0445 \u0437\u043d\u0430\u043a\u043e\u0432 <i>(\u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f)<\/i><br \/>  \u0422\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u043e <i>(spring)<\/i><\/code><br \/>  \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/zj\/9a\/2a\/zj9a2am-qcrnwn1ipcjddjgkn20.png\" alt=\"image\">  <\/p>\n<h3>Spring MVC <\/h3>\n<p>  \u041a\u043e\u043d\u0435\u0447\u043d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u044d\u0442\u043e \u0432\u0441\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0432 web \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438.<br \/>  \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u043f\u0440\u043e\u0435\u043a\u0442 Controller, jsp \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 (\u0442\u0443\u0442 \u043a\u0441\u0442\u0430\u0442\u0438 \u043c\u043e\u0433\u0443\u0442 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0441\u0442\u0440\u0430\u043d\u0438\u0446 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e freeMarker, \u0438 \u0434\u0440.), css \u0441\u0442\u0438\u043b\u044c, pom \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c. \u0418 \u0442\u0430\u043a \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443<br \/>  1) MVC Controller  <\/p>\n<pre><code class=\"java\">import org.springframework.validation.Validator;  @Controller public class DemoJValidationController {      @Autowired     @Qualifier(\"personValidator\") \/\/ spring validator     private Validator personValidator;      @InitBinder     protected void initBinder(WebDataBinder binder) {         binder.setValidator(personValidator);     }      @GetMapping(\"\/\")     public String savePersonAction(ModelMap model) {         model.addAttribute(\"person\", new Person(null, null));         return \"personEdit\";     }      @RequestMapping(value = \"\/save\", method = RequestMethod.POST)     public String savePersonAction(             @Valid @ModelAttribute(\"person\") Person person,             BindingResult bindingResult, Model model) {         if (bindingResult.hasErrors()) {             return \"personEdit\"; \/\/ to person.jsp page         }          model.addAttribute(\"name\", person.getName());         model.addAttribute(\"age\", person.getAge());         return \"saveSuccess\"; \/\/ to saveSuccess.jsp page     }      @RequestMapping(value = \"\/edit\", method = RequestMethod.POST)     public String editPersonAction(ModelMap model) {         model.addAttribute(\"person\", new Person(null, null));         return \"personEdit\"; \/\/ to personEdit.jsp page;     }  } <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e spring injection \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d PersonValidator<br \/>  <code> @Autowired<br \/>   @Qualifier(\"personValidator\") \/\/ spring validator<br \/>   private Validator personValidator;<br \/>  <\/code><br \/>  \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c PersonValidator \u0432 initBinder <br \/>  <code> @InitBinder<br \/>   protected void initBinder(WebDataBinder binder) {<br \/>   binder.setValidator(personValidator);<br \/>   }<br \/>  <\/code><br \/>  \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 <code>@Valid<\/code><br \/>  \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e spring \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0438\u0433\u043d\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u044b. <br \/>  \u0415\u0441\u043b\u0438 \u0443\u0431\u0440\u0430\u0442\u044c \u0438\u0437 \u043a\u043e\u0434\u0430<br \/>  <code> @InitBinder<br \/>   protected void initBinder(WebDataBinder binder) <\/code><br \/>  \u0442\u043e \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u0441\u044f \u0432\u0441\u0435 \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438, \u0430 spring \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0438\u0433\u043d\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u044b.<br \/>  \u0427\u0442\u043e \u0431\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0441\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0438 \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u0438 spring, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u0442\u0430\u043a: <br \/>  \u0423\u0431\u0440\u0430\u0442\u044c @InitBinder, \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c injection<br \/>  <code> @Autowired<br \/>   @Qualifier(\"personValidator\") \/\/ spring validator<br \/>   private Validator personValidator;<br \/>  <\/code><br \/>  \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432 spring \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0432\u0440\u0443\u0447\u043d\u0443\u044e<br \/>  <code> \/\/ spring validate<br \/>   personValidator.validate(person, bindingResult);<br \/>  <\/code>\u0412\u043e\u0442 \u043a\u043e\u0434:  <\/p>\n<pre><code class=\"java\">@Controller public class DemoJValidationController {      @Autowired     @Qualifier(\"personValidator\") \/\/ spring validator     private Validator personValidator;     \/\/...     @RequestMapping(value = \"\/save\", method = RequestMethod.POST)     public String savePersonAction(             @Valid @ModelAttribute(\"person\") Person person,             BindingResult bindingResult, Model model) {         \/\/ spring validate         personValidator.validate(person, bindingResult);          if (bindingResult.hasErrors()) {             return \"personEdit\"; \/\/ to person.jsp page         }          model.addAttribute(\"name\", person.getName());         model.addAttribute(\"age\", person.getAge());         return \"saveSuccess\"; \/\/ to saveSuccess.jsp page     } } <\/code><\/pre>\n<p>  \u0442.\u0435. \u0432 bindingResult \u0431\u0443\u0434\u0443\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u044b \u0435\u0449\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043e\u0442 spring :-), \u0447\u0442\u043e \u0438 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c! <br \/>  \u041f\u0440\u0438\u0432\u044f\u0437\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 jsp \u0438 \u043c\u043e\u0434\u0435\u043b\u0438, \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u043c &#8212;<code> modelAttribute=\"person\"<\/code> \u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0430 SpringMVC\u2019s Form Tag Library.<br \/>  \u041e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">DemoJValidationApplication<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"java\">@SpringBootApplication @ImportResource(\"classpath:configuration.xml\") public class DemoJValidationApplication {  \tpublic static void main(String[] args) { \t\tSpringApplication.run(DemoJValidationApplication.class, args); \t} }<\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  Spring configuration  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">configuration.xml<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt; &lt;beans xmlns=\"http:\/\/www.springframework.org\/schema\/beans\"        xmlns:mvc=\"http:\/\/www.springframework.org\/schema\/mvc\"        xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xmlns:beans=\"http:\/\/www.springframework.org\/schema\/c\"        xsi:schemaLocation=\"         http:\/\/www.springframework.org\/schema\/beans         http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd         http:\/\/www.springframework.org\/schema\/mvc         http:\/\/www.springframework.org\/schema\/mvc\/spring-mvc.xsd\"&gt;       &lt;bean id=\"messageSource\" class=\"org.springframework.context.support.ReloadableResourceBundleMessageSource\"&gt;         &lt;property name=\"basename\" value=\"classpath:message\"\/&gt;         &lt;property name=\"defaultEncoding\" value=\"UTF-8\"\/&gt;     &lt;\/bean&gt;      &lt;mvc:annotation-driven\/&gt;     &lt;mvc:resources mapping=\"\/resources\/**\" location=\"classpath:\/META-INF\/resources\/\"\/&gt; &lt;\/beans&gt; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">personEdit.jsp<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"html\">&lt;%@ page language=\"java\"          contentType=\"text\/html; charset=UTF-8\"          pageEncoding=\"UTF-8\"%&gt; &lt;%@ taglib prefix=\"form\" uri=\"http:\/\/www.springframework.org\/tags\/form\" %&gt; &lt;%@ taglib prefix=\"c\" uri=\"http:\/\/java.sun.com\/jsp\/jstl\/core\" %&gt; &lt;html&gt; &lt;head&gt;     &lt;link href=\"&lt;c:url value=\"\/resources\/my.css\" \/&gt;\" rel=\"stylesheet\"&gt;     &lt;title&gt;Person&lt;\/title&gt; &lt;\/head&gt; &lt;body&gt; &lt;h3&gt;     Enter Person. &lt;\/h3&gt;      &lt;form:form method=\"POST\" modelAttribute=\"person\" action=\"save\"&gt;         &lt;div&gt;         Name:         &lt;form:input path=\"name\"\/&gt;         &lt;form:errors path=\"name\" cssClass=\"error\"\/&gt;         &lt;\/div&gt;          &lt;div&gt;         Age:         &lt;form:input path=\"age\"\/&gt;         &lt;form:errors path=\"age\" cssClass=\"error\"\/&gt;         &lt;\/div&gt;          &lt;button type=\"submit\"&gt;Registration&lt;\/button&gt;     &lt;\/form:form&gt;  &lt;\/body&gt; &lt;\/html&gt; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">saveSuccess.jsp<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"html\">&lt;%@ page language=\"java\"          contentType=\"text\/html; charset=UTF-8\"          pageEncoding=\"UTF-8\"%&gt; &lt;%@ taglib prefix=\"form\" uri=\"http:\/\/www.springframework.org\/tags\/form\" %&gt; &lt;%@ taglib prefix=\"c\" uri=\"http:\/\/java.sun.com\/jsp\/jstl\/core\" %&gt; &lt;html&gt; &lt;head&gt;     &lt;link href=\"&lt;c:url value=\"\/resources\/my.css\" \/&gt;\" rel=\"stylesheet\"&gt;     &lt;title&gt;Person Saved Successfully&lt;\/title&gt; &lt;\/head&gt; &lt;body&gt; &lt;h3&gt;     Person Saved Successfully. &lt;\/h3&gt;  &lt;form:form method=\"POST\" modelAttribute=\"person\" action=\"edit\"&gt;     &lt;div&gt;         ${name}     &lt;\/div&gt;     &lt;div&gt;         ${age}     &lt;\/div&gt;     &lt;button type=\"submit\"&gt;Edit&lt;\/button&gt; &lt;\/form:form&gt; &lt;\/body&gt; &lt;\/html&gt; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">my.css<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"css\">span.error {     color: red; } form div{     margin: 5px; } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">pom \u0444\u0430\u0439\u043b<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt; &lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" \txsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt; \t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;  \t&lt;groupId&gt;com.example&lt;\/groupId&gt; \t&lt;artifactId&gt;DemoJSRvalidation&lt;\/artifactId&gt; \t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt; \t&lt;packaging&gt;jar&lt;\/packaging&gt;  \t&lt;name&gt;DemoJSRvalidation&lt;\/name&gt; \t&lt;description&gt;Demo project for Spring Boot JSR-303 validation&lt;\/description&gt;  \t&lt;parent&gt; \t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t&lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt; \t\t&lt;version&gt;2.0.5.RELEASE&lt;\/version&gt; \t\t&lt;relativePath\/&gt; &lt;!-- lookup parent from repository --&gt; \t&lt;\/parent&gt;  \t&lt;properties&gt; \t\t&lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt; \t\t&lt;project.reporting.outputEncoding&gt;UTF-8&lt;\/project.reporting.outputEncoding&gt; \t\t&lt;java.version&gt;1.8&lt;\/java.version&gt; \t&lt;\/properties&gt;  \t&lt;dependencies&gt; \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-boot-starter-validation&lt;\/artifactId&gt; \t\t&lt;\/dependency&gt;  \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-boot-starter-test&lt;\/artifactId&gt; \t\t\t&lt;scope&gt;test&lt;\/scope&gt; \t\t&lt;\/dependency&gt;  \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt; \t\t&lt;\/dependency&gt;  \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.hibernate&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;hibernate-validator&lt;\/artifactId&gt; \t\t\t&lt;version&gt;4.1.0.Final&lt;\/version&gt; \t\t&lt;\/dependency&gt;  \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.apache.tomcat.embed&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;tomcat-embed-jasper&lt;\/artifactId&gt; \t\t&lt;\/dependency&gt; \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;javax.servlet&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;jstl&lt;\/artifactId&gt; \t\t&lt;\/dependency&gt; \t&lt;\/dependencies&gt;  \t&lt;build&gt; \t\t&lt;plugins&gt; \t\t\t&lt;plugin&gt; \t\t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t\t&lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt; \t\t\t&lt;\/plugin&gt;  \t\t&lt;\/plugins&gt; \t&lt;\/build&gt; &lt;\/project&gt; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/i4\/7d\/kl\/i47dkln5xjabcsdk01a-zwcwq0i.png\" alt=\"image\"><br \/>  \u0420\u0430\u0431\u043e\u0442\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/gt\/uj\/t0\/gtujt01tiiiv8uqdyiayfhjpjx8.gif\" alt=\"image\"><br \/>  \u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u044b<br \/>  <a href=\"https:\/\/beanvalidation.org\/2.0\/spec\/#validationapi-validatorapi\">Bean Validation specification<\/a><\/div>\n<p>        <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d()}}}t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>     <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/post\/424819\/\"> https:\/\/habr.com\/post\/424819\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u0430 (bean) \u0432 java \u0442\u0435\u043c\u0430 \u043d\u0435 \u043d\u043e\u0432\u0430\u044f, \u043d\u043e \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u0430\u044f \u0438 \u0437\u0434\u0435\u0441\u044c \u044f \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044e \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0430\u0441\u043f\u0435\u043a\u0442\u044b: \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 JSR-303, \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0438\u0441\u0442\u043e \u0432 Java \u0438 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Spring, \u043a\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0438 \u0432 Web.<br \/>  \u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435: \u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 (JSR-303) \u0432<br \/>   \u2014 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u043c Java \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438<br \/>   \u2014 c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Spring<br \/>   \u2014 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 Java + Spring<br \/>   \u2014 Spring MVC   <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-290744","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/290744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=290744"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/290744\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=290744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=290744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=290744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}