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?

Serandel
  • 417
  • 5
  • 20
  • Can't you just add the jar? – K.Nicholas Jun 28 '17 at 16:16
  • 2
    Possible duplicate of [Configuring ObjectMapper in Spring](https://stackoverflow.com/questions/7854030/configuring-objectmapper-in-spring) – ledniov Jun 28 '17 at 16:16
  • 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. – Serandel Jun 28 '17 at 16:31
  • Spring boot without web starter does not initializes ObjectMapper. It is silly, they intend you will not use JSON outside of HTTP: https://github.com/spring-projects/spring-boot/issues/10031 – Leos Literak Mar 23 '19 at 15:59

3 Answers3

18

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

Fervento
  • 286
  • 3
  • 4
4

Why don't you just initialize it yourself.

@Bean
ObjectMapper objectMapper() {
    return new ObjectMapper();
}
Leonard Brünings
  • 12,408
  • 1
  • 46
  • 66
  • 1
    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. – Serandel Jun 29 '17 at 07:20
  • 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. – Leonard Brünings Jun 29 '17 at 11:57
  • 1
    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? – liyuhui Nov 08 '18 at 09:27
2

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

Strelok
  • 50,229
  • 9
  • 102
  • 115