I Spring Boot 2.x er det så simpelt som at oprette en registreringsbean, der registrerer alle dine konvertere:
@Configuration
public class Converters {
@Bean
public MongoCustomConversions mongoCustomConversions() {
return new MongoCustomConversions(
Arrays.asList(
new MyClassToBytesConverter(),
new BytesToMyClassConverter()));
}
}
Opret derefter dine konverterklasser:
@WritingConverter
public class MyClassToBytesConverter implements Converter<MyClass, Binary> {
@Override
public Binary convert(MyClasssource) {
// your code
}
}
@ReadingConverter
public class BytesToMyClassConverter implements Converter<Binary, MyClass> {
@Override
public MyClass convert(Binary source) {
/// your code
}
}