[英]Getting null value in property list from yaml in spring boot
I'm working on a service and trying to read a property from a yaml file.我正在开发一项服务并尝试从 yaml 文件中读取属性。 I'm Getting a null in some on thems even if the value is set.即使设置了值,我也会在某些人身上得到 null。 This are the properties in yaml file:这是 yaml 文件中的属性:
property-combos:
status:
- codigo: ATIVA
descricao: teste 1
- codigo: CONCLUIDA
descricao: teste 2
- codigo: EXPIRADA
descricao: teste 3
I'm reading the roperties with the following classes:我正在阅读以下课程的绳索:
@Component
@ConfigurationProperties("property-combos")
@Validated
public class StatusProperites {
@Valid
protected final List<StatusDataItemProperties> status = new ArrayList<>();
public List<StatusDataItemProperties> getStatus() {
return status;
}
}
This is the POJO这是 POJO
@Validated
public class StatusDataItemProperties {
@NotNull
protected String codigo;
@NotNull
protected String descricao;
public String getCodigo() {
return codigo;
}
public void setCodigo(String codigo) {
this.codigo = codigo;
}
public String getDecricao() {
return descricao;
}
public void setDecricao(String descricao) {
this.descricao = descricao;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof StatusDataItemProperties)) return false;
StatusDataItemProperties that = (StatusDataItemProperties) o;
return Objects.equals(getCodigo(), that.getCodigo()) &&
Objects.equals(getDecricao(), that.getDecricao());
}
@Override
public int hashCode() {
return Objects.hash(getCodigo(), getDecricao());
}
}
And this is the component where i get the values from:这是我从中获取值的组件:
@Component
public class StatusTransacaoComponent {
private static final Logger LOG = LoggerFactory.getLogger(StatusTransacaoComponent.class);
private static final String SUCESSO = "000";
@Autowired
StatusProperites statusProperites;
public List<StatusDataItemProperties> getStatusCombo(){
return statusProperites.getStatus();
}
}
Problem: The probles is when the app is starting the properties are readed, but the values are like this:问题:问题是应用程序启动时读取属性,但值如下:
codigo: ATIVA
descricao: null
And this happens with all of these properties所有这些属性都会发生这种情况
Tried solutions: I´ve tried to change variable and property name, but always I'm getting the null value in constant descricao.尝试过的解决方案:我尝试更改变量和属性名称,但总是在常量描述中获得 null 值。
Question: What could be happening and how can i resolve this, thanks.问题:可能会发生什么,我该如何解决,谢谢。
You have typos in getter and setter, please change to the following:你的getter和setter有错别字,请改成如下:
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
If you add this dependency to your POM如果您将此依赖项添加到您的 POM
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
you can annotate your data class with @Data
and skip getters/setters.您可以使用@Data
注释您的数据 class 并跳过 getter/setter。
You have code mistakes in getDecricao and setDecricao, should correct like this您在 getDecricao 和 setDecricao 中有代码错误,应该像这样更正
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.