0

I want to write a custom converter in Spring Data JDBC like this -

@ReadingConverter
public class IntegerToStringConverter implements Converter<Integer, String> {

    @Override
    public String convert(Integer source) {
         return source != null && source == 0 ? "ABC" : "XYZ";
    }
}

I want this conversion to be applied only on a few fields of the response as in all Integers should not be converted to String using the above reading converter rule? Spring Data JDBC Documentation doesn't mention any restricted conversion example

zb226
  • 9,586
  • 6
  • 49
  • 79
  • I have slightly modified the title of my question is there a way to define a field-specific converter in Spring data Jdbc – Mohit Gupta Oct 08 '21 at 07:42

1 Answers1

0

You currently cannot define field specific converters in Spring Data (JDBC or otherwise).

But if you replace your String by a custom class in your domain model you can just register an appropriate converter and make your model more expressive as well, since it now would be obvious that the field doesn't accept an arbitrary String.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348