How to deserialize Spring Boot actuator environment

Clash Royale CLAN TAG#URR8PPP
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:
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?
Environment
That's interesting. So I'm basically wasting my time.
– Gep
Aug 8 at 11:34
The
Environment is a heavy and custom object to instantiate, especially the internal PropertySource instances. You cannot simply deserialize them.– M. Deinum
Aug 8 at 11:35
Environment
PropertySource
1 Answer
1
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:
org.springframework.core.env.Environment
ObjectMapper
ObjectMapper
org.springframework.core.env.Environment e = mapper.readValue(is,org.springframework.core.env.Environment.class);
org.springframework.core.env.Environment.class
org.springframework.core.env.StandardEnvironment
Map<String, Object> map = mapper.readValue(is,HashMap<String, Object>.class);
And then go from there
Thank you guys. I don't have the source code of the service I'm trying to inspect, so I don't know what kind of environment is being returned. Looking at the json I would guess a StandardServletEnvironment: {"activeProfiles":["aaaaa","bbbbbbbbbbb"],"propertySources":["name":"server.ports","properties":"local.server.port":"value":50053,"name":"servletContextInitParams","properties":,{"name":"systemProperties","properties":{"java.runtime.name":{"value":"Java(TM) SE Runtime... Anyway, whether I use StandardServletEnvironment or StandardEnvironment I get the same error:
– Gep
Aug 8 at 11:28
Can not deserialize instance of org.springframework.core.env.MutablePropertySources out of START_ARRAY token at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@67f9fca4; line: 1, column: 42] (through reference chain: org.springframework.web.context.support.StandardServletEnvironment["propertySources"])
– Gep
Aug 8 at 11:30
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
That isn't the environment object but a representation of what Spring Boot calls the environment. Trying to deserialize that into one of the
Environmentclasses simply won't work.– M. Deinum
Aug 8 at 11:19