<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)  編輯  收藏 所屬分類: SPRING 、SPRING INTERGRATIONSPRING BOOT 、Bean Validation

    主站蜘蛛池模板: 国产精品1024在线永久免费| 99re这里有免费视频精品| 亚洲午夜久久久影院| 四虎国产精品永久免费网址| 亚洲色欲色欲www| 亚洲五月午夜免费在线视频| 91成人在线免费观看| jizzjizz亚洲日本少妇| 亚洲AV无码成人精品区天堂 | 亚洲婷婷天堂在线综合| 国产一级一片免费播放i| 久久精品视频免费播放| 亚洲AV无码专区亚洲AV桃| 亚洲国产精品无码久久久蜜芽 | 丰满人妻一区二区三区免费视频| 亚洲成av人片在线看片| 亚洲一区视频在线播放| 久久精品免费一区二区喷潮| 毛片基地看看成人免费| 亚洲色偷偷色噜噜狠狠99| 久久精品国产亚洲AV果冻传媒| 日本不卡视频免费| 最近2019年免费中文字幕高清| 欧洲美女大片免费播放器视频| 亚洲熟妇av一区二区三区下载| 337p日本欧洲亚洲大胆裸体艺术| 午夜寂寞在线一级观看免费| 久久成人免费大片| 一区二区三区免费视频播放器| 中文无码亚洲精品字幕| 99人中文字幕亚洲区| 国产精品V亚洲精品V日韩精品| 性色av免费观看| 91成年人免费视频| 三级毛片在线免费观看| 美女扒开屁股让男人桶爽免费| 亚洲一级毛片免费观看| 伊人久久综在合线亚洲2019| 最新国产AV无码专区亚洲 | 亚洲性色成人av天堂| 国产V亚洲V天堂无码|