0
@Converter(autoApply = true)
public class Test implements AttributeConverter<TestObj, String> {

  @Autowired
  private ObjectMapper mapper;

  @Override
  public String convertToDatabaseColumn(TestObj object) {
    try {
      return mapper.writeValueAsString(object);
    } catch (JsonProcessingException e) {
      e.printStackTrace();
      return null;
    }
  }

  @Override
  public TestObj convertToEntityAttribute(String s) {
    try {
      return mapper.readValue(s, TestObj.class);
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
  }
}

The above code causes NullPointerException as the mapper object is not initialized. It worked when I didn't add,

@Converter(autoApply = true)

Any idea how do I get this working? I am using this in a spring boot application.

Malathi
  • 2,119
  • 15
  • 40

1 Answers1

3

@Autowired annotation only works when used inside a bean managed by Spring. Yet your class Test is only annotated with @Converter which is java pure annotation. Please try with @Component spring annotation on your Test class.

zpavel
  • 951
  • 5
  • 11