06 — Advanced

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.

How It Works

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

SpringConverterAdapter

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()
    }
}

Registering Custom Converters

You have two options for adding custom converters:

Option 1: Spring Converter Beans

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);
    }
}

Option 2: Rulii Converter Beans

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

Disabling Default Converters

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.