Код: Выделить всё
org.springframework.boot
spring-boot-starter-parent
3.3.2
...
...
org.springframework.boot
spring-boot-starter-validation
...
Код: Выделить всё
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Currency;
@Getter
@Setter
@ConfigurationProperties(prefix = "my-config")
public class ConfigProperties {
private Currency currency;
}
Далее я установил следующее значение в application.yml:
Код: Выделить всё
my-config:
currency: 840
Код: Выделить всё
import jakarta.annotation.Nonnull;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.util.Currency;
@Component
@ConfigurationPropertiesBinding
public class IntegerToCurrencyConverter implements Converter {
@Override
public Currency convert(@Nonnull Integer source) {
return Currency.getInstance(String.valueOf(source));
}
}
Код: Выделить всё
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under 'my-config.currency' to java.util.Currency:
Property: my-config.currency
Value: "840"
Origin: class path resource [application.yaml] - 33:13
Reason: failed to convert java.lang.Integer to java.util.Currency (caused by java.lang.IllegalArgumentException)
Action:
Update your application's configuration
Нашёл аналогичная проблема была на GitHub, но она была закрыта.
Подробнее здесь: https://stackoverflow.com/questions/787 ... til-curren