My project uses the 3.3.2 version of Spring
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.2</version>
<relativePath/>
</parent>
...
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
...
<dependencies>
I have defined a configuration for myself with the Currency
field and use the java.util.Currency
type:
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;
}
I have set @EnableConfigurationProperties(ConfigProperties.class)
in the configuration.
Next, I set the following value in application.yml:
my-config:
currency: 840
For correct conversion, I wrote the following class:
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<Integer, Currency> {
@Override
public Currency convert(@Nonnull Integer source) {
return Currency.getInstance(String.valueOf(source));
}
}
But I still get the error:
***************************
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
I did it according to the tutorial https://www.baeldung.com/configuration-properties-in-spring-boot
I found a similar issue on GitHub, but it was closed.
bifurcated is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.