@Validated @Valid 的区别?
    前者支持分组;方法级别
    后者支持嵌套;
    是否还有别的区别?
    自定义校验注解实现?

    区别:
    https://www.cnblogs.com/xiaoqi/p/spring-valid.html
    官方文档:
    https://docs.spring.io/spring-framework/docs/4.1.x/spring-framework-reference/html/validation.html

    Spring 的实现:

    1. @NotBlank
    2. The annotated element must not be null and must contain at least one non-whitespace character.
    3. @NotEmpty
    4. The annotated element must not be null nor empty.
    5. Supported types are:
    6. - CharSequence (length of character sequence is evaluated)
    7. - Collection (collection size is evaluated)
    8. - Map (map size is evaluated)
    9. - Array (array length is evaluated)
    10. @NotNull
    11. The annotated element must not be null. Accepts any type.
    12. @Size
    13. The annotated element size must be between the specified boundaries (included).
    14. Supported types are:
    15. - CharSequence (length of character sequence is evaluated)
    16. - Collection (collection size is evaluated)
    17. - Map (map size is evaluated)
    18. - Array (array length is evaluated)
    19. null elements are considered valid.
    20. @Future
    21. The annotated element must be an instant, date or time in the future.
    22. Now is defined by the ClockProvider attached to the Validator or ValidatorFactory. The default clockProvider defines the current time according to the virtual machine, applying the current default time zone if needed.
    23. Supported types are:
    24. java.util.Date
    25. java.util.Calendar
    26. java.time.Instant
    27. java.time.LocalDate
    28. java.time.LocalDateTime
    29. java.time.LocalTime
    30. java.time.MonthDay
    31. java.time.OffsetDateTime
    32. java.time.OffsetTime
    33. java.time.Year
    34. java.time.YearMonth
    35. java.time.ZonedDateTime
    36. java.time.chrono.HijrahDate
    37. java.time.chrono.JapaneseDate
    38. java.time.chrono.MinguoDate
    39. java.time.chrono.ThaiBuddhistDate
    40. null elements are considered valid.

    正则表达式校验:@Pattern

    1. /*
    2. * Jakarta Bean Validation API
    3. *
    4. * License: Apache License, Version 2.0
    5. * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
    6. */
    7. package javax.validation.constraints;
    8. import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
    9. import static java.lang.annotation.ElementType.CONSTRUCTOR;
    10. import static java.lang.annotation.ElementType.FIELD;
    11. import static java.lang.annotation.ElementType.METHOD;
    12. import static java.lang.annotation.ElementType.PARAMETER;
    13. import static java.lang.annotation.ElementType.TYPE_USE;
    14. import static java.lang.annotation.RetentionPolicy.RUNTIME;
    15. import java.lang.annotation.Documented;
    16. import java.lang.annotation.Repeatable;
    17. import java.lang.annotation.Retention;
    18. import java.lang.annotation.Target;
    19. import javax.validation.Constraint;
    20. import javax.validation.Payload;
    21. import javax.validation.constraints.Pattern.List;
    22. /**
    23. * The annotated {@code CharSequence} must match the specified regular expression.
    24. * The regular expression follows the Java regular expression conventions
    25. * see {@link java.util.regex.Pattern}.
    26. * <p>
    27. * Accepts {@code CharSequence}. {@code null} elements are considered valid.
    28. *
    29. * @author Emmanuel Bernard
    30. */
    31. @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
    32. @Retention(RUNTIME)
    33. @Repeatable(List.class)
    34. @Documented
    35. @Constraint(validatedBy = { })
    36. public @interface Pattern {
    37. /**
    38. * @return the regular expression to match
    39. */
    40. String regexp();
    41. /**
    42. * @return array of {@code Flag}s considered when resolving the regular expression
    43. */
    44. Flag[] flags() default { };
    45. /**
    46. * @return the error message template
    47. */
    48. String message() default "{javax.validation.constraints.Pattern.message}";
    49. /**
    50. * @return the groups the constraint belongs to
    51. */
    52. Class<?>[] groups() default { };
    53. /**
    54. * @return the payload associated to the constraint
    55. */
    56. Class<? extends Payload>[] payload() default { };
    57. /**
    58. * Possible Regexp flags.
    59. */
    60. public static enum Flag {
    61. /**
    62. * Enables Unix lines mode.
    63. *
    64. * @see java.util.regex.Pattern#UNIX_LINES
    65. */
    66. UNIX_LINES( java.util.regex.Pattern.UNIX_LINES ),
    67. /**
    68. * Enables case-insensitive matching.
    69. *
    70. * @see java.util.regex.Pattern#CASE_INSENSITIVE
    71. */
    72. CASE_INSENSITIVE( java.util.regex.Pattern.CASE_INSENSITIVE ),
    73. /**
    74. * Permits whitespace and comments in pattern.
    75. *
    76. * @see java.util.regex.Pattern#COMMENTS
    77. */
    78. COMMENTS( java.util.regex.Pattern.COMMENTS ),
    79. /**
    80. * Enables multiline mode.
    81. *
    82. * @see java.util.regex.Pattern#MULTILINE
    83. */
    84. MULTILINE( java.util.regex.Pattern.MULTILINE ),
    85. /**
    86. * Enables dotall mode.
    87. *
    88. * @see java.util.regex.Pattern#DOTALL
    89. */
    90. DOTALL( java.util.regex.Pattern.DOTALL ),
    91. /**
    92. * Enables Unicode-aware case folding.
    93. *
    94. * @see java.util.regex.Pattern#UNICODE_CASE
    95. */
    96. UNICODE_CASE( java.util.regex.Pattern.UNICODE_CASE ),
    97. /**
    98. * Enables canonical equivalence.
    99. *
    100. * @see java.util.regex.Pattern#CANON_EQ
    101. */
    102. CANON_EQ( java.util.regex.Pattern.CANON_EQ );
    103. //JDK flag value
    104. private final int value;
    105. private Flag(int value) {
    106. this.value = value;
    107. }
    108. /**
    109. * @return flag value as defined in {@link java.util.regex.Pattern}
    110. */
    111. public int getValue() {
    112. return value;
    113. }
    114. }
    115. /**
    116. * Defines several {@link Pattern} annotations on the same element.
    117. *
    118. * @see Pattern
    119. */
    120. @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
    121. @Retention(RUNTIME)
    122. @Documented
    123. @interface List {
    124. Pattern[] value();
    125. }
    126. }

    之前还用过 hibernate 的实现;
    image.png