<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 閱讀(337) 評論(0)  編輯  收藏 所屬分類: SPRING 、SPRING INTERGRATIONSPRING BOOT 、Bean Validation

    主站蜘蛛池模板: 国产亚洲精久久久久久无码77777 国产亚洲精品成人AA片新蒲金 | 欧美三级在线电影免费| avtt天堂网手机版亚洲| 美女被免费网站在线视频免费| 亚洲A丁香五香天堂网| 小日子的在线观看免费| 中文字幕亚洲综合久久综合| 亚洲国产香蕉人人爽成AV片久久| 嫩草在线视频www免费观看| 欧洲 亚洲 国产图片综合| 亚洲国产高清精品线久久| 99re6热视频精品免费观看| 亚洲JIZZJIZZ妇女| 亚洲av中文无码乱人伦在线咪咕| 国内免费高清在线观看| aaa毛片免费观看| 亚洲另类无码专区首页| 亚洲爆乳无码专区| 国产又大又粗又硬又长免费 | A级毛片成人网站免费看| 亚洲国产精品yw在线观看| 亚洲成av人片一区二区三区| 亚洲免费电影网站| 一个人看的免费高清视频日本| 亚洲妇女水蜜桃av网网站| 日韩中文字幕精品免费一区| 国产精品免费久久久久影院 | 亚洲综合成人婷婷五月网址| 国产亚洲一区二区在线观看| 免费看大美女大黄大色| 91精品成人免费国产片| aaa毛片免费观看| 免费精品久久久久久中文字幕| 亚洲伊人久久综合影院| 最新猫咪www免费人成| 一级毛片在线免费看| 一级毛片无遮挡免费全部| 亚洲av永久无码精品网址| 亚洲最大在线视频| 久久久久亚洲精品无码系列| 国产亚洲人成A在线V网站|