简体   繁体   中英

How to deserialize Spring Boot actuator environment

I would like to deserialize the Spring Boot Environment object returned by:

http://hostname:port/actuator/env

I'm using Jackson's 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.

Which class should I use?

org.springframework.core.env.Environment is an interface. So ObjectMapper can not guess what concrete class to instantiate. You need to tell your ObjectMapper which class to use. So in your line 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. For example org.springframework.core.env.StandardEnvironment (depending on what kind of environment actually being returned). Otherwise de-serialize it to map:

Map<String, Object> map = mapper.readValue(is,HashMap<String, Object>.class);

And then go from there

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM