Jan 9th, 2013, 04:47 AM
Support for Json (Jackson) Serialization within a service (not web controller)
as we all know, there is a nice support of Jackson JSON Serialization in Spring Web, which is easy to use within Web Controllers (@ResponseBody etc.). In my situation, I want to reuse the configured Jackson Environment in order to serialize to JSON within my service.
I do not want to create an ObjectMapper every time, I want to use the Jackson Library. Is there any easy way to reuse the configured ObjectMapper which is automatically used, when using Json in a WebController?
Jan 9th, 2013, 07:17 AM
What is wrong with simply injecting the ObjectMapper? Configure the ObjectMapper and inject it into your service.
Jan 9th, 2013, 07:21 AM
Of course I can register my own ObjectMapper as a bean and then autowire it. I was searching for a way to avoid configuring a second ObjectMapper, as there must be an already configured one, which is used automatically when used in the WebController (with @ResponseBody).
Originally Posted by Marten Deinum
Jan 9th, 2013, 07:32 AM
No there isn't... That is constructed internally and not registered as a bean... However the spring components allow for the ObjectMapper to be injected so you could explicitly configure one and inject it into the spring components also.
Jan 9th, 2013, 08:12 AM
Perfect! Thanks for your help!