I need to use default ObjectMapper inside my Spring-boot application as a singleton instance. Can I simply @autowire the ObjectMapper(Which instance created by default in Spring-boot application) inside my application without creating a @Bean(As I don't want to change any functionalities of ObjectMapper)
Asked
Active
Viewed 1.7k times
3
-
3yes you can autowire – pvpkiran Feb 17 '18 at 19:00
-
@pvpkiran By autowire will it return the same ObjectMapper bean used by default in Spring-boot. – Udara Gunathilake Feb 18 '18 at 02:24
-
So it will act like a singleton one right..?? – Udara Gunathilake Feb 18 '18 at 06:00
-
2Yes it will return the same instance – pvpkiran Feb 18 '18 at 07:07
-
@pvpkiran Is there any way(doc) to find the beans that created default by the spring-boot application..? – Udara Gunathilake Feb 18 '18 at 07:50
-
No, there is no such doc. There are way too many. You can just Autowire ApplicationContext in a class and get all the beans – pvpkiran Feb 19 '18 at 09:26
3 Answers
3
You don't have to change the funcuntallty, you can just return the Default ObjectMapper
@Configuration
public class ObjectMapperConfig {
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public ObjectMapper objectMapper(){
return new ObjectMapper();
}
}

Daniel Taub
- 5,133
- 7
- 42
- 72
-
If I don't want to create an ObjectMapperConfig class and return a bean like your sample. Can I just autowire the default objectMapper which is already created by SpringBoot? – Udara Gunathilake Feb 26 '18 at 05:20
-
-
-
1If you want to autowire the objectmapper, and spring didn’t create already a beam of that, then yes – Daniel Taub Feb 26 '18 at 06:16
-
Yes, That's I really wanted to know. I listed out the bean list and ObjectMapper is also created by Spring. So this means I don't to create a separate bean right.? – Udara Gunathilake Feb 26 '18 at 07:54
3
TL;DR
Yes, you can.
Explanation
The reason for that is that Spring uses "auto-configuration" and will instantiate that bean for you (as was mentioned) if you haven't created your own. The "instantiation" logic resides inside JacksonAutoConfiguration.java
(github link). As you can see, it is a @Bean
annotated method with @ConditionalOnMissingBean
annotation where the magic happens. It's no different than any other beans auto span up by Spring.

skryvets
- 2,808
- 29
- 31
1
If you know something else is creating it, yes you can just autowire and use it in your bean
@Lazy
@Autowired
ObjectMapper mapper;
@PostConstruct
public ObjectMapper configureMapper() {
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
mapper.configure(MapperFeature.ALLOW_COERCION_OF_SCALARS, true);
mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
SimpleModule module = new SimpleModule();
module.addDeserializer(LocalDate.class, new LocalDateDeserializer());
module.addSerializer(LocalDate.class, new LocalDateSerializer());
mapper.registerModule(module);
return mapper;
}

Kalpesh Soni
- 6,879
- 2
- 56
- 59