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

4

9

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?

Cheeky answered 28/6, 2017 at 16:14 Comment(4)
Can't you just add the jar?Piloting
Possible duplicate of Configuring ObjectMapper in SpringClaribelclarice
Thing is, Spring Boot autoconfigures the ObjectMapper and it can be tuned by several well documented configuration properties. If I create the ObjectMapper as the other question propose, all of this should be done by hand, if I'm not mistaken.Cheeky
Spring boot without web starter does not initializes ObjectMapper. It is silly, they intend you will not use JSON outside of HTTP: github.com/spring-projects/spring-boot/issues/10031Undesirable
S
20

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).

Samurai answered 5/6, 2019 at 12:48 Comment(1)
This worked for me. Thank you. Is the optimal solution going forward?Prothorax
H
4

Why don't you just initialize it yourself.

@Bean
ObjectMapper objectMapper() {
    return new ObjectMapper();
}
Hearthside answered 29/6, 2017 at 1:9 Comment(3)
My problem is, as I said in a previous comment, that Spring Boot autoconfigures ObjectMapper with several well documented configuration properties. If I instance it by my self, all of this tuning is lost, if I'm not wrong.Cheeky
Yes you need to do it by hand, but it is not that hard, and looking at the source code of Jackson2ObjectMapperBuilder it doesn't seem to set any default values. It does auto detect some well-known modules but adding them by hand is not that hard. If you really want to, you could copy the java into your code, it is ASL2, but I would just use my own object wrapper.Neukam
But this should be work just by using @Autowired ObjectMapper objectMapper. If we initialize it by ourselves, this wouldn't be a smart solution right?Hereld
A
2

You can try and JUST add org.springframework:spring-web dependency as that class is located in it.

Albertinealbertite answered 29/6, 2017 at 1:3 Comment(2)
But if this is just a java application rather than web application, what should I do?Hereld
github.com/spring-projects/spring-boot/issues/10031 has some background and justification for this.Chitterlings
K
0

Make sure to have the spring-boot-starter-web dependency in your pom.xml or build.gradle as it includes the ObjectMapper and spring-boot-starter-json and spring-web dependencies etc..

Keslie answered 17/9 at 23:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.