英文:
@Min and @Max validation is not working of hibernate validator package in spring boot
问题
@Min 和 @Max 验证器不起作用,因为值被分配给了属性文件中的静态变量。但它接受所有的值并且不进行验证。
**OTPLengthAndExpiryDetail.java**
```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 = "值应大于等于4")
@Max(value = 6, message = "值应小于等于6")
@NotNull(message = "不能为空。请提供4到6之间的数字")
@Value("${otp.length}")
private static Integer length;
@Min(value = 20, message = "值应大于等于20")
@Max(value = 180, message = "值应小于等于180")
@NotNull(message = "不能为空。请提供20到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. ################ 数据库详细信息 ###############################
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. ############## 应用程序的日志详细信息 #################
spring.h2.console.enabled=true
#logging.level.org.hibernate=debug
spring.jpa.show-sql=true
############################################################################
#3.############### 用于OTP的TWILLO详细信息 #######################
#下面两个Twillo详细信息不能为空
twilio.accountSID=AC53bec33dc8bae99f8
twilio.authId=7c31ef0e28e75473
twilio.phoneNumber=16468634753
############################################################################
#4.############### OTP配置详细信息 #######################
#otp.length不能为空。请提供4到6之间的数字
otp.length=4
#otp.expirytime不能为空。请提供20到300之间的数字
otp.expiryTime=2000
otp.message=你的验证码是
############################################################################
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;
}
}
英文:
@Min and @Max validator is not working as the value is getting assigned to a static variable from the properties file. But it taking all the value and not validating.
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 * it is the class where I am instantiating the custom Configuration*
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;
}
}
答案1
得分: 7
根据 JSR-303 的第3章第1节:
> 静态字段和静态方法不参与验证。
因此,如果您希望对 length
和 expiryTime
应用验证,不应将它们指定为 static
字段。
英文:
According to chapter 3 section 1 of JSR-303:
> Static fields and static methods are excluded from validation.
Therefore, length
and expiryTime
should not be designated as static
fields if you wish to apply validation to them.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论