如何在不从spring-boot-starter-web继承的情况下在Spring Boot中获得ObjectMapper实例?

Ser*_*del 5 spring spring-mvc jackson spring-boot jackson-databind

我在玩Spring Boot 2.0.0M2,试图创建一个CLI应用程序,而不是一个Web应用程序。我的问题是,即使包括

  compile 'org.springframework.boot:spring-boot-starter'
  compile 'org.springframework.boot:spring-boot-starter-json'
Run Code Online (Sandbox Code Playgroud)

在我的构建中,ObjectMapper不是由Boot创建的,因为

Bean method 'jacksonObjectMapper' not loaded because @ConditionalOnClass did not find required class 'org.springframework.http.converter.json.Jackson2ObjectMapperBuilder'
Run Code Online (Sandbox Code Playgroud)

当您想拥有Spring Boot实例并配置Jackson而又不想启动Web服务器时,最佳实践是什么?

Leo*_*ngs 5

你为什么不自己初始化它。

@Bean
ObjectMapper objectMapper() {
    return new ObjectMapper();
}
Run Code Online (Sandbox Code Playgroud)


Fer*_*nto 5

spring-boot-starter-json如果要避免包括完整的Web堆栈,则可以添加(自Spring Boot v.2.0.0M4起)。