Convertisseurs built-in
Ravel fournit des convertisseurs (priorité 1) pour ces types, appliqués automatiquement pendant config.getValue() et config.getOptionalValue() :
| Type | Comportement |
|---|---|
|
Pass-through (pas de conversion) |
|
Valeurs truthy : |
|
Via |
|
Via |
|
Via |
|
Via |
|
Via |
|
Convertit la valeur en |
|
Format ISO-8601 : |
|
Format ISO-8601 : |
|
Format ISO : |
|
Format ISO : |
|
Format ISO : |
|
Format ISO : |
|
Format ISO : |
|
Format ISO : |
|
Format ISO : |
|
Via |
Types Enum |
Correspondance par nom de constante d’énumération (sensible à la casse) |
|
CSV : valeurs séparées par des virgules, converties en liste de |
Tableaux
// microprofile-config.properties
app.items=foo,bar,baz
app.ids=1,2,3,4
// Code
String[] items = config.getValue("app.items", String[].class);
// Résultat : ["foo", "bar", "baz"]
Integer[] ids = config.getValue("app.ids", Integer[].class);
// Résultat : [1, 2, 3, 4]
Collections
List<String> items = config.getValue("app.items", List.class);
Set<Integer> ids = config.getValue("app.ids", Set.class);
Collection<Duration> timeouts = config.getValue("app.timeouts", Collection.class);
Convertisseurs implicites
Si un type ne dispose pas d’un convertisseur built-in, Ravel tente d’utiliser un convertisseur implicite (priorité 2) :
-
Constructeur public prenant
String:new MyType(string) -
Méthode statique
valueOf(String):MyType.valueOf(string) -
Méthode statique
parse(CharSequence):MyType.parse(charseq)
public class MyDuration {
private final long millis;
// Ce constructeur public permet la conversion implicite
public MyDuration(String iso8601) {
this.millis = Duration.parse("PT" + iso8601).toMillis();
}
}
// microprofile-config.properties
app.timeout=1H30M
// Code
MyDuration timeout = config.getValue("app.timeout", MyDuration.class);
// Appelle en interne : new MyDuration("1H30M")
Convertisseurs personnalisés
Enregistrez un Converter<T> personnalisé pour les types non couverts par les convertisseurs built-in ou implicites :
import org.eclipse.microprofile.config.spi.Converter;
public class ColorConverter implements Converter<Color> {
@Override
public Color convert(String value) {
if (value == null || value.isEmpty()) {
throw new IllegalArgumentException("Color cannot be empty");
}
if (value.startsWith("#")) {
// Format hexadécimal : #RRGGBB
return Color.decode(value);
} else if (value.equalsIgnoreCase("red")) {
return Color.RED;
} else if (value.equalsIgnoreCase("green")) {
return Color.GREEN;
} else if (value.equalsIgnoreCase("blue")) {
return Color.BLUE;
}
throw new IllegalArgumentException("Unknown color: " + value);
}
}
Priorité des convertisseurs
Lors de la résolution d’un type, Ravel applique les convertisseurs dans cet ordre :
-
Convertisseurs built-in (priorité 1)
-
Convertisseurs implicites (priorité 2)
-
Convertisseurs personnalisés via ServiceLoader (priorité >= 3)
-
Convertisseurs de
ConfigBuilder.withConverter()(priorité la plus élevée)
Le premier convertisseur qui déclare gérer le type est utilisé. Si aucun ne correspond, une IllegalArgumentException est levée.
Exemple : Convertisseur JSON
public class JsonConverter<T> implements Converter<T> {
private final ObjectMapper mapper;
private final Class<T> targetType;
public JsonConverter(Class<T> type) {
this.targetType = type;
this.mapper = new ObjectMapper(); // ou votre bibliothèque de sérialisation
}
@Override
public T convert(String value) {
try {
return mapper.readValue(value, targetType);
} catch (Exception e) {
throw new IllegalArgumentException("Invalid JSON for " + targetType.getName(), e);
}
}
}
// Usage
MyConfigClass config = configProvider.getConfig()
.getValue("app.config.json", MyConfigClass.class);
Suivant
-
Expressions de propriété — référencer d’autres valeurs de configuration
-
Intégration CDI — injecter les valeurs converties via
@ConfigProperty