This question already has an answer here:
Is there anyway to do something like this:
Type type = new TypeToken<MyHappyClass>(){}.getType();
List<type> = new LinkedList<type>();
thanks in advance
========================================================================
I'll try to explain better what I need, hope I'm not doing anything ridiculous
I'm using restTemplate.getForObject to get data from a webservice. However they use a custom MediaType, therefore I am implementing a CustomMessageConverter to restTemplate understand that MediaType and convert it to my data object.
The problem is that this data object use generics. All objects returned from this webservice have some common fields and some specific, so, to map this and not repeat the code I created a GeneralItem and each resource extends from Item.
So, my MessageConverter is created this way:
public class MyMessageConverter<T extends Item> extends AbstractHttpMessageConverter<T>
And my readInternal goes like this:
@Override
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
InputStream istream = inputMessage.getBody();
String responseString = IOUtils.toString(istream);
Type type = new TypeToken<GeneralItem<?>>(){}.getType();
GeneralItem<?> resource = new Gson().fromJson(responseString, type);
return (T) resource.getItem();
}
But that doesnt work because I need the clazz type instead of the wildcard (*) for gson to parse it. Am I going the wrong way?
Aucun commentaire:
Enregistrer un commentaire