【发布时间】:2018-08-08 10:17:31
【问题描述】:
我想反序列化返回的 Spring Boot Environment 对象:
http://hostname:port/actuator/env
我正在使用杰克逊的ObjectMapper:
private static final ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
...
ClientResponse clientResponse = resource.type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
InputStream is = clientResponse.getEntityInputStream();
org.springframework.core.env.Environment e = mapper.readValue(is, org.springframework.core.env.Environment.class);
上面的代码失败并出现以下错误,这是有道理的:
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of org.springframework.core.env.Environment, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
但是我已经尝试了Environment 类的所有实现(AbstractEnvironment、MockEnvironment、StandardEnvironment、StandardServletEnvironment),它们也都失败了。
我应该使用哪个类?
【问题讨论】:
-
这不是环境对象,而是 Spring Boot 所称环境的表示。试图将其反序列化为
Environment类之一根本行不通。 -
这很有趣。所以我基本上是在浪费时间。
-
Environment是一个很重的自定义对象,尤其是内部的PropertySource实例。你不能简单地反序列化它们。
标签: java spring-boot jackson spring-boot-actuator