Skip to content

Type Conversion

rulii has its own type conversion system (ConverterRegistry), and Spring has its own (ConversionService). rulii-spring bridges them with SpringConverterAdapter, so converters from both worlds work inside your rules.

The auto-configured ConverterRegistry bean does three things during initialization:

  1. Registers default converters — rulii’s built-in converters (controlled by the rulii.converts.registerDefaults property)
  2. Registers custom Converter beans — any rulii Converter<S, T> beans in the context are added to the registry
  3. Wraps Spring’s ConversionService — if a ConversionService bean is present, it’s wrapped in a SpringConverterAdapter and registered as a catch-all converter

This adapter implements rulii’s Converter<Object, Object> interface and delegates to Spring’s ConversionService. Its source and target types are both Object, which means it acts as a fallback converter — if no specific rulii converter matches, the adapter checks whether Spring can handle the conversion.

public class SpringConverterAdapter implements Converter<Object, Object> {
public boolean canConvert(Type source, Type target) {
// Delegates to conversionService.canConvert()
}
public Object convert(Object source, Type targetType) {
// Delegates to conversionService.convert()
}
}

You have two options for adding custom converters:

Register a Spring @Component that implements org.springframework.core.convert.converter.Converter. These are picked up by Spring’s ConversionService and automatically available to rulii through the adapter.

@Component
public class StringToMoneyConverter
implements org.springframework.core.convert.converter.Converter<String, Money> {
@Override
public Money convert(String source) {
return Money.parse(source);
}
}

Register a bean that implements rulii’s org.rulii.convert.Converter<S, T>. These are directly added to the ConverterRegistry.

If you only want your custom converters and nothing else, set:

application.properties
rulii.converts.registerDefaults=false

When set to false, rulii’s built-in converters are not registered. Only your custom converter beans and the Spring ConversionService adapter (if present) will be used.