java - @Min 和 @Max 验证在 spring boot 中不适用于 hibernate validator 包

标签 java spring spring-boot hibernate spring-mvc

@Min 和 @Max validator 不起作用,因为该值正在从属性文件分配给静态变量。但它采取了所有的值(value),而不是验证。
OTPLengthAndExpiryDetail.java

package com.custom.store.sms.twillo.model;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

@Component
@Validated
public class OTPLengthAndExpiryDetail {
    
    @Min(value = 4 , message = "Value should be greater then then equal to 4")
    @Max(value = 6 , message = "Value should be less then then equal to 6")
    @NotNull(message = "It can not be null. Please provide no. in b/w 4 to 6")
    @Value("${otp.length}")
    private static Integer length;
    
    @Min(value = 20 , message = "Value should be greater then equal to 20")
    @Max(value = 180 , message = "Value should be less then equal to 180")
    @NotNull(message = "It can not be null. Please provide no. in b/w 20 to 300")
    @Value("${otp.expiryTime}")
    private static Integer expiryTime;

    public static Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        OTPLengthAndExpiryDetail.length = length;
    }

    public static Integer getExpiryTime() {
        return expiryTime;
    }

    public void setExpiryTime(Integer expiryTime) {
        OTPLengthAndExpiryDetail.expiryTime = expiryTime;
    }
    
}

application.properties
#1. ################       DB DETAILS        ###############################
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://remotemysql.com/q5UV1n69DW?useSSL=false
spring.datasource.username=q5
spring.datasource.password=ur
############################################################################



#2. ##############     LOGGING DETAILS FOR APPLICATION     #################
spring.h2.console.enabled=true
#logging.level.org.hibernate=debug
spring.jpa.show-sql=true
############################################################################



#3.###############       TWILLO DETAILS FOR OTP      #######################
#both below twillo details can't be null                        
twilio.accountSID=AC53bec33dc8bae99f8                                     
twilio.authId=7c31ef0e28e75473
twilio.phoneNumber=16468634753                    
############################################################################



#4.###############     OTP CONFIGURATION DETAILS     #######################
#otp.length can not be null. Please provide no. in b/w 4 to 6
otp.length=4
#otp.expirytime can not be null. Please provide no. in b/w 20 to 300
otp.expiryTime=2000
otp.message=your otp is 
############################################################################

build.gradle
plugins {
    id 'org.springframework.boot' version '2.3.3.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

group = 'com.custom.store'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation group: "com.twilio.sdk", name: "twilio", version : "7.47.2"
    implementation('org.springframework.boot:spring-boot-starter-validation')
    runtimeOnly 'mysql:mysql-connector-java'
    compileOnly 'org.projectlombok:lombok'
    //implementation "org.hibernate:hibernate-validator:6.1.5.Final"
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

OTPProperties.java * 它是我实例化自定义配置的类*
package com.custom.store.sms.twillo.conf;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import com.custom.store.sms.twillo.model.OTPLengthAndExpiryDetail;
import com.custom.store.sms.twillo.model.TwilioAccountAndAuthDetail;


@Component
@ConfigurationProperties
@PropertySource("classpath:application.properties")
public class OTPProperties {
    
    //@Autowired
    TwilioAccountAndAuthDetail twilio;
    
    //@Autowired
    OTPLengthAndExpiryDetail otp;

    public TwilioAccountAndAuthDetail getTwilio() {
        return twilio;
    }

    public void setTwilio(TwilioAccountAndAuthDetail twilio) {
        this.twilio = twilio;
    }

    public OTPLengthAndExpiryDetail getOtp() {
        return otp;
    }

    public void setOtp(OTPLengthAndExpiryDetail otp) {
        this.otp = otp;
    }
    
}

最佳答案

根据JSR-303的第3章第1节:

Static fields and static methods are excluded from validation.


因此,lengthexpiryTime不应指定为 static字段,如果您希望对它们应用验证。

关于java - @Min 和 @Max 验证在 spring boot 中不适用于 hibernate validator 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63677890/

相关文章:

java - 如何清理 gwt CellList?

java - 无需应用程序测试 Spring/Spring Boot

java - Spring 测试 : Rollback the database after testing the dao

java - 尚未找到 JAXB-API 的实现(运行 java jar 时)

java - 如何将 catalina.out 文件与嵌入式 tomcat 链接起来

java - 类的构造函数参数是否也应该使用 Java Bean Validation API 注释进行注释?

java - 如何在 Java Web 应用程序中实现复杂的页面流

java - Spring 表单 ModelAttribute 字段验证,避免 400 Bad Request Error

java - 页面之间重定向 - Spring MVC

java - JSON元素从单个变为列表