<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(339) 評論(0)  編輯  收藏 所屬分類: SPRINGSPRING INTERGRATIONSPRING BOOTBean Validation

    主站蜘蛛池模板: 免费一级毛片正在播放| 亚洲国产另类久久久精品| 色爽黄1000部免费软件下载| 亚洲一区精品无码| 久久九九兔免费精品6| 亚洲GV天堂无码男同在线观看| 亚洲一区二区三区乱码A| 2015日韩永久免费视频播放| 亚洲另类自拍丝袜第五页 | 亚洲人精品午夜射精日韩| 国产麻豆一精品一AV一免费| 2020久久精品亚洲热综合一本 | **一级一级毛片免费观看| 国产偷国产偷亚洲高清在线| 亚洲电影免费在线观看| 国产又黄又爽又猛的免费视频播放 | 猫咪社区免费资源在线观看| h片在线观看免费| 亚洲中字慕日产2020| 亚洲欧洲日产国码av系列天堂| 色妞WWW精品免费视频| A国产一区二区免费入口| 亚洲国产精品免费观看| 亚洲AV永久精品爱情岛论坛| 国产乱子伦片免费观看中字| 99精品国产成人a∨免费看| 老妇激情毛片免费| 亚洲人成电影网站| 国产亚洲一区二区三区在线观看| 精品久久免费视频| 67pao强力打造高清免费| 一二三四在线观看免费中文在线观看| 亚洲三级视频在线观看| 国产av无码专区亚洲av桃花庵| 免费人成视频在线观看视频| 91麻豆最新在线人成免费观看| 国产一级高青免费| 国产亚洲精彩视频| 亚洲av永久无码精品秋霞电影秋| 久久久久亚洲av无码专区| 亚洲日韩欧洲无码av夜夜摸|