哪个bean在Spring Boot中将对象转换为Json?

Gus*_*ave 2 json spring-boot

我有一个Spring Boot(MVC)应用程序.我需要在代码中从Map转换为Json(而不是从Web请求返回结果时).但我想使用Spring自动执行的相同bean.哪个bean正在这样做?

pvp*_*ran 5

Spring使用Jackson ObjectMapper类来执行Json序列化和反序列化.

您可以在要使用的类中自动装配Objectmapper.然后你就可以做到

mapper.writeValueAsString(...)//You can pass any object to this method.   
Run Code Online (Sandbox Code Playgroud)

new ObjectMapper()如果要在独立应用程序中执行此操作,可以使用创建新实例.但是因为你正在使用spring-bootm,所以ObjectMapper bean已经存在,你可以只使用Autowire