I have a method that, when using a normal PoJo works just fine. But
converting to a generic method, the call to response.getEntity(new
GenericType<List<T>>(){}) throws the ever popular
ClientHandlerExcpetion:
A message body reader for Java class java.util.List, and Java type
java.util.List<T>, and MIME media type application/json was not found
Here's my code.
public <T> List<T> getObjects(T t, String path){
WebResource r = client.resource(props.props.getResourceServerPath());
// Get JAXB response
ClientResponse response = r.path(path)
.header(HttpHeaders.AUTHORIZATION, accessTokenAsHeader())
.accept(MediaType.APPLICATION_JSON)
.get(ClientResponse.class);
if(response.getClientResponseStatus() == ClientResponse.Status.OK){
List<T> list = response.getEntity(new GenericType<List<T>>(){});
StringBuilder sb = new StringBuilder();
sb.append(String.format("\tNo. of %1$ss: %2#s", t.getClass().getName(),
list.size()));
logger.info(sb.toString());
return list;
}
else{
ResourceServerError error =
response.getEntity(ResourceServerError.class);
logger.info(String.format("Resource Server returned error: %1#s - %2#s",
error.getStatus(), error.getMessage()));
return null;
}
}
While the above code throws an exception, the following works just fine:
ClientResponse response = r.path(path)
.header(HttpHeaders.AUTHORIZATION, accessTokenAsHeader())
.accept(MediaType.APPLICATION_JSON)
.get(ClientResponse.class);
if(response.getClientResponseStatus() == ClientResponse.Status.OK){
List<Account> list = response.getEntity(new
GenericType<List<Account>>(){});
StringBuilder sb = new StringBuilder();
sb.append(String.format("\tNo. of %1$ss: %2#s", t.getClass().getName(),
list.size()));
logger.info(sb.toString());
return list;
}
Getting this working will provide a significant reduction in code complexity
and replication. So I hope someone has a nifty trick I can use!
TIA
--
View this message in context: http://jersey.576304.n2.nabble.com/Using-GenericType-in-a-generic-method-tp6650758p6650758.html
Sent from the Jersey mailing list archive at Nabble.com.