<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

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

    只要在JAVA BEAN中需要驗(yàn)證的字段加@NotNull這種標(biāo)簽,然后在SERVISE中的輸入?yún)?shù)中加@Valid標(biāo)簽,則就激活驗(yàn)證流程。
    也可以編程的方式自己驗(yàn)證:
    @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();
        }

    }

    自定義驗(yàn)證規(guī)則
    可用標(biāo)簽來做,以下為驗(yàn)證手機(jī)號(hào)的規(guī)則:
    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 {};
    }

    如果比較復(fù)雜的驗(yàn)證規(guī)則,則參見:
    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 校驗(yàn)數(shù)據(jù)

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

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

    主站蜘蛛池模板: 亚洲一区二区三区在线| 亚洲精品无码专区久久同性男| 久久久久久亚洲精品| A毛片毛片看免费| 亚洲精品夜夜夜妓女网| 国产三级在线免费| 久久亚洲国产成人精品性色| 免费视频一区二区| 久久av无码专区亚洲av桃花岛| 久久国产乱子伦免费精品| 在线观看亚洲人成网站| 我的小后妈韩剧在线看免费高清版| 亚洲一区免费在线观看| 国产精品酒店视频免费看| 日韩a毛片免费观看| 亚洲日韩一页精品发布| 精品无码人妻一区二区免费蜜桃| 亚洲国产精品久久丫| 成人一a毛片免费视频| 久久精品亚洲日本波多野结衣| 免费一级毛片不卡在线播放| 一个人免费观看www视频| 亚洲αv在线精品糸列| 久草在视频免费福利| 国产精品亚洲专区无码WEB| 曰韩亚洲av人人夜夜澡人人爽 | 三年片在线观看免费大全电影| 亚洲精品国产电影午夜| 免费的一级黄色片| 美女巨胸喷奶水视频www免费| 久久久亚洲欧洲日产国码二区 | 亚洲精品97久久中文字幕无码| 男女一边摸一边做爽的免费视频| 亚洲一区二区在线免费观看| 拍拍拍又黄又爽无挡视频免费| 特级做a爰片毛片免费看| 亚洲三级电影网址| 国产精品va无码免费麻豆| 国产日韩一区二区三免费高清| 亚洲制服丝袜第一页| 国产亚洲精品a在线观看|