[英]How to use @ConfigProperties with a Converter class
I tried to implement a custom config type and it worked.我尝试实现自定义配置类型并且它起作用了。 However, when I use the custom type with a group of config using the @ConfigProperties
it fails to automatically recognize the property by its name and instead treats the property as an object with a nested property.但是,当我使用@ConfigProperties
将自定义类型与一组配置一起使用时,它无法通过名称自动识别该属性,而是将该属性视为具有嵌套属性的对象。
How can I implement such a behavior correctly?我怎样才能正确地实现这样的行为? (I am new to Quarkus, so please correct me if I am doing something wrong here) (我是 Quarkus 的新手,所以如果我在这里做错了什么,请纠正我)
Here is a code snippet that converts a custom type:这是一个转换自定义类型的代码片段:
public class Percentage {
private double percentage;
public Percentage() {}
public Percentage(double percentage) {
this.percentage = percentage;
}
public void setPercentage(double percentage) {
this.percentage = percentage;
}
public double getPercentage() {
return this.percentage;
}
}
@Priority(300)
public class PercentageConverter implements Converter<Percentage> {
@Override
public Percentage convert(String value) {
int percentIndex = value.indexOf("%");
return new Percentage(Double.parseDouble(value.substring(0, percentIndex - 1)));
}
}
/// this works ------
public class Hello {
@ConfigProperty(name = "custom.vat")
Percentage vat;
public Hello () {
}
// .....
}
/// however, this fails
@ConfigProperties(prefix = "custom")
public class CustomConfig {
public Percentage vat;
public Percentage profit;
}
javax.enterprise.inject.spi.DeploymentException: No config value of type [double] exists for: custom.vat.percentage
at io.quarkus.arc.runtime.ConfigRecorder.validateConfigProperties(ConfigRecorder.java:39)
Unfortunately, I believe this does not work because Quarkus @ConfigProperties, handles these cases as if they were subgroups and try to map nested properties with the configuration (and not use the Converter).不幸的是,我认为这不起作用,因为 Quarkus @ConfigProperties 将这些情况视为子组并尝试使用配置映射嵌套属性(而不是使用转换器)。
Feel free to open up an issue in Quarkus GH if you feel this should change: https://github.com/quarkusio/quarkus/issues如果您觉得这应该改变,请随时在 Quarkus GH 中打开一个问题: https : //github.com/quarkusio/quarkus/issues
Alternately, you can use SR Config @ConfigMapping: https://smallrye.io/docs/smallrye-config/mapping/mapping.html .或者,您可以使用 SR 配置 @ConfigMapping: https ://smallrye.io/docs/smallrye-config/mapping/mapping.html。 It covers a few more cases, including direct Conversion and in the future it may replace Quarkus @ConfigProperties.它涵盖了更多情况,包括直接转换,将来它可能会取代 Quarkus @ConfigProperties。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.