简体   繁体   English

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

[英]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. 我正在玩Spring Boot 2.0.0M2,试图创建一个CLI应用程序,而不是一个Web应用程序。 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 在我的构建中,ObjectMapper不是由Boot创建的

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? 当您想要Spring Boot实例并配置Jackson而不想启动Web服务器时,最佳做法是什么?

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依赖,因为该类位于其中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在没有 spring-boot-starter-web 的情况下配置 Spring Security AuthenticationManager - Configure Spring Security AuthenticationManager without spring-boot-starter-web 如何限制 spring-boot-starter-web 中的完整有效负载大小? - How to limit full payload size in spring-boot-starter-web? 使用spring-boot-starter-web的Spring Boot无法在Maven中编译 - Spring Boot with spring-boot-starter-web fails to compile in maven spring initializr:spring-boot-starter vs spring-boot-starter-web - spring initializr: spring-boot-starter vs spring-boot-starter-web @CrossOrigin 不适用于 spring-boot-starter-web 2.5.2 - @CrossOrigin not working for spring-boot-starter-web 2.5.2 结合 ObjectDB 使用 Spring-Boot-Starter-Web - Using Spring-Boot-Starter-Web in combination with ObjectDB spring-boot-starter-web @Autowired无效 - spring-boot-starter-web @Autowired isn't working 使用 spring-boot-starter-web “找不到可接受的表示” - "Could not find acceptable representation" using spring-boot-starter-web spring-boot-starter-web和spring-boot-starter-web-services以及spring-boot-starter-jersey之间的区别 - Difference between spring-boot-starter-web and spring-boot-starter-web-services and spring-boot-starter-jersey 如何在不继承'spring-boot-starter-parent'模块的情况下测试我的Spring-Boot应用程序? - How can I test my Spring-Boot application without inheriting 'spring-boot-starter-parent' module?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM