JacksonParametricType construct is deprecated, but constructParameterizedType does not work the same

Here is my code snippet, and the new "constructParameterizedType" does not fit my needs (unless I am missing something that I assume I am). I have a generic class called Result, where T is any base class that extends my base class "Inflatable". represents the data records returned from the Salesforce REST API ... so here is a sample code that works:

Class c = Class.forName("sfshare.UserRecord" );
JavaType type = mapper.getTypeFactory().constructParametricType(Result.class, c);
Result<T> res = mapper.readValue(rspData, type);

But if I use the new (not obsolete) constructParameterizedType () method, the same code will not compile because it does not match the constructParameterizedType parameters. But constructParameterizedType is not used much yet, and there are no examples for use ... only Javadoc - which does not make sense for my use case.

+4
source share
2 answers

If you look at the arguments and, in particular, Javadocs, you will notice that there is a new type: the 2nd argument is the target "target" for the parameters. For example, if you want to build an equivalent:

ArrayList<String>

what do you want to pass as arguments:

constructParameterizedType(ArrayList.class, List.class, String.class)

, , Collection.class . , .

"" , Iterable<T>: .

, .

+3

:

Class c = Class.forName("sfshare.UserRecord");
TypeFactory typeFactory = mapper.getTypeFactory();
JavaType type = typeFactory.constructParametrizedType(Result.class, Result.class, c);
Result<T> res = mapper.readValue(rspData, type);

Result<T> :

JavaType type = typeFactory.constructParametrizedType(Result.class, ResultInterface.class, c);
+3

All Articles