Java 如果使用构造函数绑定数据,如何返回TypeMismatchException的自定义消息
我正在尝试发送TypeMismatchException的自定义消息。但无法执行此操作,因为数据是使用构造函数绑定绑定的。有没有办法发送自定义消息 下面的application-messages.properties otp.expiryTime是整数字段,但是如果我用一些字符初始化它,那么它会抛出typeMisMatch异常,因此我想发送属性文件中为typeMisMatch分配的自定义消息 应用程序消息。属性Java 如果使用构造函数绑定数据,如何返回TypeMismatchException的自定义消息,java,spring,spring-boot,hibernate,spring-mvc,Java,Spring,Spring Boot,Hibernate,Spring Mvc,我正在尝试发送TypeMismatchException的自定义消息。但无法执行此操作,因为数据是使用构造函数绑定绑定的。有没有办法发送自定义消息 下面的application-messages.properties otp.expiryTime是整数字段,但是如果我用一些字符初始化它,那么它会抛出typeMisMatch异常,因此我想发送属性文件中为typeMisMatch分配的自定义消息 应用程序消息。属性 #1.############### OTP CONFIGURATION D
#1.############### 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=a
otp.message=Your otp is
############################################################################
#2.############### TWILLO DETAILS FOR OTP #######################
#both below twillo details can't be null
twilio.accountSID=AC53bec33
twilio.authId=7c31ef0e28e754734
twilio.phoneNumber=16468634753
############################################################################
typeMismatch="{0}" is invalid.
typeMismatch.int="{0}" must be an integer.
typeMismatch.double="{0}" must be a double.
typeMismatch.float="{0}" must be a float.
typeMismatch.long="{0}" must be a long.
typeMismatch.short="{0}" must be a short.
typeMismatch.java.lang.Integer="{0}" must be an integer.
typeMismatch.java.lang.Double="{0}" must be a double.
typeMismatch.java.lang.Float="{0}" must be a float.
typeMismatch.java.lang.Long="{0}" must be a long.
typeMismatch.java.lang.Short="{0}" must be a short.
typeMismatch.java.util.Date="{0}" is not a date.
otPlengtHandExpirydeail.java它是绑定数据的类
package com.custom.store.sms.otp.twillo.model;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.TypeMismatchException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
@Component
@Validated
@ConstructorBinding
@PropertySource("classpath:application-messages.properties")
public class OTPLengthAndExpiryDetail {
@Range(min = 4,max = 6,message = "Expiry time should be in b/w 4 to 6")
@NotNull(message = "Otp length can not be null. Please provide no. in b/w 4 to 6")
private final Integer length;
@Range(min = 20,max = 180,message = "Expiry time should be in b/w 20 to 180")
@NotNull(message = "Otp expiry time can not be null. Please provide the value in b/w 20 to 300 seconds")
private final Integer expiryTime;
@NotNull(message = "Message can't be null")
@Size(max = 1500,message = "Message length can't be greater then 1500")
private final String message;
public OTPLengthAndExpiryDetail(@Value("${otp.length}") Integer length,
@Value("${otp.expiryTime}") Integer expiryTime,
@Value("${otp.message}") String message) {
this.length = length;
this.expiryTime = expiryTime;
this.message = message;
System.out.println(message);
}
public Integer getLength() {
return length;
}
public Integer getExpiryTime() {
return expiryTime;
}
public String getMessage() {
return message;
}
}