java—如果使用构造函数绑定数据,如何返回typemismatchexception的自定义消息

yfjy0ee7  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(232)

我正在尝试发送typemismatchexception的自定义消息。但无法做到这一点,因为数据是使用构造函数绑定绑定的。有没有办法发送自定义消息。
下面的application-messages.properties otp.expirytime是整数字段,但是如果我用一些字符初始化它,那么它会引发typemissmatch异常,因此我想发送属性文件中为typemissmatch指定的自定义消息。
应用程序消息.properties


# 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.

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;
    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题