[英]How to deserialize Spring Boot actuator environment
I would like to deserialize the Spring Boot Environment object returned by: 我想反序列化由以下方式返回的Spring Boot Environment对象:
http://hostname:port/actuator/env HTTP://主机名:端口/致动器/ env的
I'm using Jackson's ObjectMapper
: 我正在使用Jackson的
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);
The code above fails with the following error, which makes sense: 上面的代码失败,并显示以下错误,这是有道理的:
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
But I've tried all the implementations of the Environment
class ( AbstractEnvironment
, MockEnvironment
, StandardEnvironment
, StandardServletEnvironment
) and they all fail as well. 但是我已经尝试了
Environment
类的所有实现( AbstractEnvironment
, MockEnvironment
, StandardEnvironment
, StandardServletEnvironment
),并且它们都失败了。
Which class should I use? 我应该使用哪个班级?
org.springframework.core.env.Environment
is an interface. org.springframework.core.env.Environment
是一个接口。 So ObjectMapper
can not guess what concrete class to instantiate. 因此,
ObjectMapper
无法猜测要实例化的具体类。 You need to tell your ObjectMapper
which class to use. 您需要告诉
ObjectMapper
使用哪个类。 So in your line org.springframework.core.env.Environment e = mapper.readValue(is,org.springframework.core.env.Environment.class);
所以在你的行
org.springframework.core.env.Environment e = mapper.readValue(is,org.springframework.core.env.Environment.class);
You need to replace org.springframework.core.env.Environment.class
with some concrete class. 您需要用一些具体的类替换
org.springframework.core.env.Environment.class
。 For example org.springframework.core.env.StandardEnvironment
(depending on what kind of environment actually being returned). 例如
org.springframework.core.env.StandardEnvironment
(取决于实际返回的环境类型)。 Otherwise de-serialize it to map: 否则,将其反序列化以映射:
Map<String, Object> map = mapper.readValue(is,HashMap<String, Object>.class);
And then go from there 然后从那里去
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.