Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get an ObjectMapper instance in Spring Boot without inheriting from spring-boot-starter-web?

I'm playing around with Spring Boot 2.0.0M2, trying to create a CLI application, not a web one. My problem is that even including

  compile 'org.springframework.boot:spring-boot-starter'
  compile 'org.springframework.boot:spring-boot-starter-json'

in my build, ObjectMapper is not created by Boot because

Bean method 'jacksonObjectMapper' not loaded because @ConditionalOnClass did not find required class 'org.springframework.http.converter.json.Jackson2ObjectMapperBuilder'

What's the best practice when you want to have Spring Boot instance and configure Jackson but don't want to start a web server?

like image 752
Serandel Avatar asked Jun 28 '17 16:06

Serandel


People also ask

Does spring boot have Jackson ObjectMapper?

Overview. When using JSON format, Spring Boot will use an ObjectMapper instance to serialize responses and deserialize requests. In this tutorial, we'll take a look at the most common ways to configure the serialization and deserialization options. To learn more about Jackson, be sure to check out our Jackson tutorial.

Should ObjectMapper be a bean?

Assuming no configuration is required, just a plain ObjectMapper will do. It's OK to be declared as static. But in environment like Spring, IMHO you should declare it as a bean.

Which is the correct starter dependency for spring boot?

Now Spring Boot Starters provides all those with just a single dependency. The official starters follow a naming convention spring-boot-starter-*, where * denotes application type. For example, if we want to build web including RESTful applications using Spring MVC we have to use spring-boot-starter-web dependency.

Does spring boot starter web include Tomcat?

Spring Boot Starter Tomcat is the default embedded container for Spring Boot Starter Web. We cannot exclude it while using web services. We can exclude it when we want to use another embedded container. It also supports Jetty Server and Undertow Server.


2 Answers

You can add spring-boot-starter-json if you want to avoid to include the full web stack (since Spring Boot v.2.0.0M4).

like image 173
Fervento Avatar answered Sep 20 '22 00:09

Fervento


Why don't you just initialize it yourself.

@Bean
ObjectMapper objectMapper() {
    return new ObjectMapper();
}
like image 45
Leonard Brünings Avatar answered Sep 19 '22 00:09

Leonard Brünings