paulwong

          JSR-303 Bean Validation

          接收數據的JAVA BEAN通常需要驗證其中字段的正確性,如不準為空,符合EMAIL格式等。
          JSR-303 Bean Validation則提供了這樣的便捷。

          只要在JAVA BEAN中需要驗證的字段加@NotNull這種標簽,然后在SERVISE中的輸入參數中加@Valid標簽,則就激活驗證流程。
          也可以編程的方式自己驗證:
          @MessageEndpoint
          //@Validated
          public class MqMessageCcdValidator {
              
              private static final Logger LOGGER = LoggerFactory.getLogger(MqMessageCcdValidator.class);
              
              @Autowired
              private Validator validator;
              
              @ServiceActivator
              public MqMessage<CcdRequest> validate(/* @Valid */ Message<MqMessage<CcdRequest>> requestMessage) {
                  Set<ConstraintViolation<MqMessage<CcdRequest>>> set = validator.validate(requestMessage.getPayload());
                  if(CollectionUtils.isNotEmpty(set)) {
                      CompositeException compositeException = new CompositeException();
                      set.forEach(
                          constraintViolation -> {
                                                      LOGGER.info("{}", constraintViolation);
                                                      ReqInfoValidationException exception =
                                                              new ReqInfoValidationException(constraintViolation.getMessage());
                                                      compositeException.addException(exception);
                                                 }
                      );
                      throw new MessageHandlingException(requestMessage, compositeException);
                  }
                  
                  return requestMessage.getPayload();
              }

          }

          自定義驗證規則
          可用標簽來做,以下為驗證手機號的規則:
          import static java.lang.annotation.RetentionPolicy.RUNTIME;

          import java.lang.annotation.ElementType;
          import java.lang.annotation.Retention;
          import java.lang.annotation.Target;

          import javax.validation.Constraint;
          import javax.validation.Payload;
          import javax.validation.ReportAsSingleViolation;
          import javax.validation.constraints.Pattern;

          @Retention(RUNTIME)
          @Target(value = { ElementType.FIELD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE })
          @Constraint(validatedBy = {})
          @ReportAsSingleViolation
          @Pattern(regexp = "^1[3-9]\\d{9}$")
          public @interface ChinaPhone {
              String message() default "Invalid Chinese mobile No.";
              Class<?>[] groups() default {};
              Class<? extends Payload>[] payload() default {};
          }

          如果比較復雜的驗證規則,則參見:
          https://reflectoring.io/bean-validation-with-spring-boot/#implementing-a-custom-validator

          How to use Java Bean Validation in Spring Boot
          https://nullbeans.com/how-to-use-java-bean-validation-in-spring-boot/

          Complete Guide to Validation With Spring Boot
          https://reflectoring.io/bean-validation-with-spring-boot/

          Spring JMS Validate Messages using JSR-303 Bean Validation
          https://memorynotfound.com/spring-jms-validate-messages-jsr-303-bean-validation/

          Spring REST Validation Example
          https://mkyong.com/spring-boot/spring-rest-validation-example/

          Spring Boot 整合 Bean Validation 校驗數據

          https://blog.csdn.net/wangzhihao1994/article/details/108403732

          posted on 2021-01-28 10:35 paulwong 閱讀(337) 評論(0)  編輯  收藏 所屬分類: SPRINGSPRING INTERGRATIONSPRING BOOTBean Validation

          主站蜘蛛池模板: 耒阳市| 拉萨市| 习水县| 张家港市| 庆云县| 麻阳| 沾益县| 古交市| 新沂市| 小金县| 彰化市| 大城县| 遂溪县| 遂平县| 佛冈县| 玉环县| 博白县| 邓州市| 古丈县| 黔江区| 青川县| 南丰县| 鄂伦春自治旗| 赤峰市| 元朗区| 宜君县| 汝城县| 响水县| 隆德县| 黎川县| 临西县| 慈利县| 乐亭县| 原阳县| 香港 | 石泉县| 四平市| 宁陵县| 玉溪市| 荃湾区| 龙井市|