Right now, I have a method in
public <T> ResponseEntity<List<T>> getProduct(String resource, Map<String, String> queryParams)
throws Exception {
return getRestTemplate().exchange(endpointAddress resource, HttpMethod.GET, new HttpEntity(createHeaders()),
new ParameterizedTypeReference<List<T>>(){}, queryParams);
}
When I call this method as ResponseEntity<List<Product>> myProduct = myServer.getProduct("someURL", myQuery)
in my Adapter class, It works fine. If I LOG.debug()
the response.getBody().toString()
I get something like <200,[{name="Terry", ID=4234, City="Dallas"}]>
My Product
class has methods such as getName()
and getID()
. I cannot simply do response.getBody().get(0).getID()
as Jackson doesn't know the type of object I'm using. I've tried doing:
ObjectMapper mapper = new ObjectMapper();
List<Product> product = mapper.convertValue(response.getBody(), new TypeReference<List<Product>>() {});
But that errors out to. On the otherhand, I can convert the body to a json
String json = mapper.writeValueAsString(wlsProduct.getBody().get(0));
and parse it to get the response, but I'd much rather use the methods within the Product
class. Is there an alternative solution or something I'm missing?
CodePudding user response:
Your body looks like <200,[valid JSON content]>
so remove from your body "200," at hte beginning and ">" at the end and you should be fine
CodePudding user response:
myServer.getProduct("someURL", myQuery)
does not return a list of Product
s, but a list of LinkedHashMap
s. You probably get a java.lang.ClassCastException
(next time you should specify the exception in your question, so we do not have to make assumptions).
your code should work as expected once you change the getProduct
method like this:
public ResponseEntity<List<Product>> getProduct(String resource, Map<String, String> queryParams)
throws Exception {
return getRestTemplate().exchange(endpointAddress resource, HttpMethod.GET, new HttpEntity(createHeaders()),
new ParameterizedTypeReference<List<Product>>(){}, queryParams);
}
why generics? your getProduct()
seems to be specific to Produt
anyway.