无法从 YAML 读取密钥
Unable to Read keys form YAML
我是 Spring Boot 的新手,我正在尝试从 application.yml
读取验证消息,但无法读取
这里是我的模特class
@Entity
@Table(name = "users_data")
public class User {
@NotBlank(message = "${errorresponse.useridMissing}")
@Column(name = "user_id")
private String userId;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
我的配置class
@Component
@ConfigurationProperties("errorresponse")
public class ErrorResponsesConfig {
private String useridMissing ;
public String getMissingValue() {
return useridMissing;
}
public void setMissingValue(String useridMissing) {
this.useridMissing = useridMissing;
}
}
application.yml
看起来像这样
spring:
jpa:
show-sql: true
hibernate:
ddl-auto: update
errorresponse:
useridMissing: Please provide UserID
但在 bean 验证期间验证失败时未读取值
当我 运行 单元测试时它给了我以下异常
javax.el.PropertyNotFoundException: ELResolver cannot handle a null base Object with identifier 'errorresponse'
at com.sun.el.lang.ELSupport.throwUnhandled(ELSupport.java:42) ~[jakarta.el-3.0.3.jar:3.0.3]
at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:100) ~[jakarta.el-3.0.3.jar:3.0.3]
at com.sun.el.parser.AstValue.getBase(AstValue.java:125) ~[jakarta.el-3.0.3.jar:3.0.3]
at com.sun.el.parser.AstValue.getValue(AstValue.java:173) ~[jakarta.el-3.0.3.jar:3.0.3]
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:183) ~[jakarta.el-3.0.3.jar:3.0.3]
at org.hibernate.validator.internal.engine.messageinterpolation.ElTermResolver.interpolate(ElTermResolver.java:67) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.validator.internal.engine.messageinterpolation.InterpolationTerm.interpolate(InterpolationTerm.java:64) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolate(ResourceBundleMessageInterpolator.java:159) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.validator.messageinterpolation.AbstractMessageInterpolator.interpolateExpression(AbstractMessageInterpolator.java:519) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.validator.messageinterpolation.AbstractMessageInterpolator.interpolateMessage(AbstractMessageInterpolator.java:415) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.validator.messageinterpolation.AbstractMessageInterpolator.interpolate(AbstractMessageInterpolator.java:355) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
at org.springframework.validation.beanvalidation.LocaleContextMessageInterpolator.interpolate(LocaleContextMessageInterpolator.java:51) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.hibernate.validator.internal.engine.validationcontext.AbstractValidationContext.interpolate(AbstractValidationContext.java:313) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.validator.internal.engine.validationcontext.AbstractValidationContext.addConstraintFailure(AbstractValidationContext.java:230) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
请指导我哪里做错了!
谢谢
您不应在 application.yml 中管理的验证或国际化消息。在 Java/Spring 应用程序中,您应该为此使用属性文件,并且语法在您的模型中没有 $:
@NotEmpty(message = "{email.notempty}")
@Email
private String email;
有关更多信息,请查看本教程:Custom Validation MessageSource in Spring Boot
我是 Spring Boot 的新手,我正在尝试从 application.yml
读取验证消息,但无法读取
这里是我的模特class
@Entity
@Table(name = "users_data")
public class User {
@NotBlank(message = "${errorresponse.useridMissing}")
@Column(name = "user_id")
private String userId;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
我的配置class
@Component
@ConfigurationProperties("errorresponse")
public class ErrorResponsesConfig {
private String useridMissing ;
public String getMissingValue() {
return useridMissing;
}
public void setMissingValue(String useridMissing) {
this.useridMissing = useridMissing;
}
}
application.yml
看起来像这样
spring:
jpa:
show-sql: true
hibernate:
ddl-auto: update
errorresponse:
useridMissing: Please provide UserID
但在 bean 验证期间验证失败时未读取值
当我 运行 单元测试时它给了我以下异常
javax.el.PropertyNotFoundException: ELResolver cannot handle a null base Object with identifier 'errorresponse'
at com.sun.el.lang.ELSupport.throwUnhandled(ELSupport.java:42) ~[jakarta.el-3.0.3.jar:3.0.3]
at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:100) ~[jakarta.el-3.0.3.jar:3.0.3]
at com.sun.el.parser.AstValue.getBase(AstValue.java:125) ~[jakarta.el-3.0.3.jar:3.0.3]
at com.sun.el.parser.AstValue.getValue(AstValue.java:173) ~[jakarta.el-3.0.3.jar:3.0.3]
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:183) ~[jakarta.el-3.0.3.jar:3.0.3]
at org.hibernate.validator.internal.engine.messageinterpolation.ElTermResolver.interpolate(ElTermResolver.java:67) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.validator.internal.engine.messageinterpolation.InterpolationTerm.interpolate(InterpolationTerm.java:64) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolate(ResourceBundleMessageInterpolator.java:159) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.validator.messageinterpolation.AbstractMessageInterpolator.interpolateExpression(AbstractMessageInterpolator.java:519) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.validator.messageinterpolation.AbstractMessageInterpolator.interpolateMessage(AbstractMessageInterpolator.java:415) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.validator.messageinterpolation.AbstractMessageInterpolator.interpolate(AbstractMessageInterpolator.java:355) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
at org.springframework.validation.beanvalidation.LocaleContextMessageInterpolator.interpolate(LocaleContextMessageInterpolator.java:51) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.hibernate.validator.internal.engine.validationcontext.AbstractValidationContext.interpolate(AbstractValidationContext.java:313) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.validator.internal.engine.validationcontext.AbstractValidationContext.addConstraintFailure(AbstractValidationContext.java:230) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
请指导我哪里做错了!
谢谢
您不应在 application.yml 中管理的验证或国际化消息。在 Java/Spring 应用程序中,您应该为此使用属性文件,并且语法在您的模型中没有 $:
@NotEmpty(message = "{email.notempty}")
@Email
private String email;
有关更多信息,请查看本教程:Custom Validation MessageSource in Spring Boot