JsonMappingException: Can not find a deserializer for non-concrete Map type

冷暖自知 提交于 2019-12-02 07:44:34

I would say the object Version need to have a field named versions of type Map<String, String> and another one named provider of type String, and you probably don't need the GenericType thing.

public class Version {

    private Map<String, String> versions;

    private String provider;

    // getters, setters, etc
}

then

Version version = commonClient.authorizedRequestBuilder(commonClient.webTarget)
            // ...
            .readEntity(Version.class);
version.getVersions().get("am"); // to get "am"

Tested with

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Map;

public class Test {
    public static void main(String[] args) throws IOException {
        String str = "{\"versions\": {\"ap\": \"Not Set\", \"am\": \"topic-test-publisher-1.0.16\", \"il\": \"topic-test-publisher-1.0.16\", \"row\": \"topic-test-publisher-1.0.49\"}, \"provider\": \"gce\"}";
        ObjectMapper objectMapper = new ObjectMapper();
        Version v = objectMapper.readValue(str, Version.class);

        System.out.println(v.versions.get("am"));
    }

    public static class Version {
        public Map<String, String> versions;

        public String provider;
    }
}

Output: "topic-test-publisher-1.0.16"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!