解决Jackson转换到泛化类型List和Map

        ResponseEntity stringResponseEntity = new RestTemplate().postForEntity("http://1.1.1.1:9080/xservices-node/nodeApi/getMachineServers", stringHttpEntity, String.class);

        System.out.println(stringResponseEntity.getBody());
        ObjectMapper objectMapper = new ObjectMapper();
        //设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        //禁止使用int代表Enum的order()來反序列化Enum,非常危險
        objectMapper.configure(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS, true);
        JavaType javaType = objectMapper.getTypeFactory().constructParametrizedType(ArrayList.class, LinkedHashMap.class, XsNodeVersion.class);
        try {
            List list = objectMapper.readValue(stringResponseEntity.getBody(), javaType);
            System.out.println(list);
        } catch (IOException e) {
            e.printStackTrace();
        }

你可能感兴趣的:(解决Jackson转换到泛化类型List和Map)