Tuesday 15 January 2013

java - Specify the return type as a parameter for a function with generics -



java - Specify the return type as a parameter for a function with generics -

i struggling generic parameters function scope. next function.

public <res, req> res makepostrequest(string postrequest, req requestbody, class<res> type) { response<type> resp = resttemplate.postforobject(postrequest, requestbody, response.class); homecoming resp.getbody(); }

jackson , json library deserialization needs homecoming type specified. otherwise response not converted right type. however, response<type> invalid. how 1 take res parameter , specify within response<res>?

note: not want utilize resttemplate.exchange. not want set http headers , create http entity. want specify generic type through parameter. help appreciated.

try this:

return resttemplate.postforobject(postrequest, requestbody, type);

or else:

responseentity<res> resp = resttemplate.postforentity(postrequest, requestbody, type); homecoming resp.getbody();

java spring generics parameters jackson

No comments:

Post a Comment