java - Unable to get a generic ResponseEntity<T> where T is a generic class "SomeClass<SomeGenericType>" -
please help me responseentity<t>
t
generic type. see of now, not supported nowdays spring resttemplate. i'm using spring mvc version 3.1.2
here code, want use: code:
responseentity<cisresponse<cisresponseentity>> res = this.resttemplate.postforentity( this.rooturl, myrequestobj, cisresponse.class);
i'm getting error:
type mismatch: cannot convert responseentity<cisresponse> responseentity<cisresponse<cisresponseentity>>
it's obvious error, how can workaround today?
than want generic response type:
cisresponse<cisresponseentity> myresponse= res.getbody(); cisresponseentity entity = myresponse.getentityfromresponse();
for now, utilize solution, postforobject()
, not postforentity()
:
cisresponse<cisresponseentity> response = this.resttemplate.postforobject( this.rooturl,myrequestobj, cisresponse.class);
this a known issue. it's fixed introduction of parameterizedtypereference
, parameterized type explicitely inherit supply type info @ runtime. called super-type token, , works around type erasure because subclasses (anoniymous in case) maintain type arguments of generic supertype @ runtime.
however can't utilize postforobject
, because api supports exchange()
:
responseentity<cisresponse<cisresponseentity>> res = template.exchange( rooturl, httpmethod.post, null, new parameterizedtypereference<cisresponse<cisresponseentity>>() {});
note lastly line demonstrates thought of super type tokens: don't supply literal cisresponse.class
, anonymous instantiation of parameterized type parameterizedtypereference<t>
, @ runtime can expected extract subtype information. can think of super type tokens hacks achieving foo<bar<baz>>.class
btw, in java don't need prefix access instance variable this
: if object defines url
, template
members, access them simple name, , not prefixing this.url
, this.template
java spring rest generics spring-mvc
No comments:
Post a Comment