简体   繁体   中英

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?

Why don't you just initialize it yourself.

@Bean
ObjectMapper objectMapper() {
    return new ObjectMapper();
}

如果要避免包含完整的Web堆栈(从Spring Boot v.2.0.0M4开始),可以添加spring-boot-starter-json

您可以尝试并添加org.springframework:spring-web依赖,因为该类位于其中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM