Reputation: 1004
I'm trying to configure a gateway using the HttpRequestExecutingMessageHandler
. The problem that I'm facing is to setExpectedResponseType
to a generic type.
I have found some solutions when you use the RestTemplate
directly, but when dealing with the configuration through the HttpRequestExecutingMessageHandler
I haven't been able to make it call the proper exchange
method of the RestTemplate
that receives the ParameterizedTypeReference
.
This is the example code:
My generic class:
public class ListWrapperModel<T> {
public ListWrapperModel() {}
private List<T> list;
}
My configuration for HttpRequestExecutingMessageHandler:
Map<String, Expression> uriVariableExp = getDefaultEndpointProperties();
SpelExpressionParser parser = new SpelExpressionParser();
uriVariableExp.put("id", parser.parseExpression("payload"));
String endpoint = "{host}/models?id={id}";
HttpRequestExecutingMessageHandler gateway;
gateway = new HttpRequestExecutingMessageHandler(endpoint, getRestTemplate());
gateway.setRequiresReply(true);
gateway.setHttpMethod(HttpMethod.GET);
gateway.setExpectedResponseType(new ParameterizedTypeReference<ListWrapperModel<Model>>(){}.getClass());
gateway.setUriVariableExpressions(uriVariableExp);
return gateway;
This is one of the scenarios that I've tried, the other one, was doing this:
gateway.setExpectedResponseType(new ParameterizedTypeReference<ListWrapperModel<Model>>(){}.getType().getClass());
But didn't work.
Debugging HttpRequestExecutingMessageHandler, I notice that when it is trying to determinate the expected type, the return type is: com.host.app.service.gateway.http.ServiceConfiguration$1
I'm new to java but I will assume that the $1
is the anonymous type created.
I would like to ask if there is a way to configure this so that can work properly.
Thanks!
Upvotes: 3
Views: 2313
Reputation: 78639
I managed to solve the problem by using a response type value expression. The following settings worked for me:
ParameterizedTypeReference<ListWrapperModel<Model>> type = new ParameterizedTypeReference<ListWrapperModel<Model>>() { };
gateway.setExpectedResponseTypeExpression(new ValueExpression<>(type));
The value expression will later be evaluated to the provided ParameterizedTypeReference
which the underlying RestTemplate
will use to convert the result.
Upvotes: 4