如何在不继承 spring-boot-starter-web 的情况下在 Spring Boot 中获取 ObjectMapper 实例?
How can I get an ObjectMapper instance in Spring Boot without inheriting from spring-boot-starter-web?
我正在玩 Spring Boot 2.0.0M2,试图创建 CLI 应用程序,而不是 Web 应用程序。我的问题是即使包括
compile 'org.springframework.boot:spring-boot-starter'
compile 'org.springframework.boot:spring-boot-starter-json'
在我的构建中,ObjectMapper 不是由 Boot 创建的,因为
Bean method 'jacksonObjectMapper' not loaded because @ConditionalOnClass did not find required class 'org.springframework.http.converter.json.Jackson2ObjectMapperBuilder'
当您想 Spring 启动实例并配置 Jackson 但不想启动 Web 服务器时,最佳做法是什么?
您可以尝试添加 org.springframework:spring-web
依赖项,因为 class 位于其中。
为什么不自己初始化呢
@Bean
ObjectMapper objectMapper() {
return new ObjectMapper();
}
如果您想避免包含完整的 Web 堆栈(自 Spring Boot v.2.0.0M4 起),您可以添加 spring-boot-starter-json
。
我正在玩 Spring Boot 2.0.0M2,试图创建 CLI 应用程序,而不是 Web 应用程序。我的问题是即使包括
compile 'org.springframework.boot:spring-boot-starter'
compile 'org.springframework.boot:spring-boot-starter-json'
在我的构建中,ObjectMapper 不是由 Boot 创建的,因为
Bean method 'jacksonObjectMapper' not loaded because @ConditionalOnClass did not find required class 'org.springframework.http.converter.json.Jackson2ObjectMapperBuilder'
当您想 Spring 启动实例并配置 Jackson 但不想启动 Web 服务器时,最佳做法是什么?
您可以尝试添加 org.springframework:spring-web
依赖项,因为 class 位于其中。
为什么不自己初始化呢
@Bean
ObjectMapper objectMapper() {
return new ObjectMapper();
}
如果您想避免包含完整的 Web 堆栈(自 Spring Boot v.2.0.0M4 起),您可以添加 spring-boot-starter-json
。