问题
How can I do like this:
Test<String> data = OBJECT_MAPPER.decodeValue("sss", Test<String>.class);
When I call this operation I get an error. I need decode generic class.
Thanks for the help.
回答1:
You can use TypeReference
. Test<String>.class
is not possible in Java
.
TypeReference testStringType = new TypeReference<Test<String>>() { };
Object value = mapper.readValue(json, testStringType);
Also works:
JavaType javaType = mapper.getTypeFactory().constructParametricType(Test.class, String.class);
Test<String> value1 = mapper.readValue(json, javaType);
See also:
- Jackson - Deserialize using generic class
- Jackson - Deserialize Generic class variable
来源:https://stackoverflow.com/questions/54946568/how-to-decode-generic-data-in-jackson