1

I am not sure how to design of my Java classes, the question is also related to better testing.

I am using static utility class/method Utils.getMapper() to get configured ObjectMapper at many places in my Spring application.

When object mapper is needed, e.g. in @Service class, should I pass the instance via constructor parameter only or is correct to get mapper in my class by calling utility method?

jaco0646
  • 15,303
  • 7
  • 59
  • 83
jnemecz
  • 3,171
  • 8
  • 41
  • 77

1 Answers1

1

It's generally better to pass the ObjectMapper instance via constructor parameter in your @Service class instead of calling the utility method to get the instance.

By passing the instance via constructor parameter, you can ensure that the ObjectMapper instance is provided explicitly to your class, which makes your class's dependencies more explicit and easier to manage.

Titoine
  • 11
  • 3