Wednesday, July 1, 2015

Read `Map` by `ObjectMapper.readValue()`

If you try to do the following:

Map<String, String> metricsMap = objectMapper.readValue(response, Map.class);

you might encounter the following exception:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

You can solve the problem by using `TypeReference` as follows:

  private final TypeReference<Map<String, String>> metricsType
      = new TypeReference<Map<String, String>>() {};

Map<String, String> metricsMap = objectMapper.readValue(response, metricsType);

No comments:

Post a Comment