简体   繁体   中英

Set the HttpRequestExecutingMessageHandler's expectedResponseType to a Page

I'm using a MessagingGateway to perform HTTP calls to another service. This endpoint returns a slice of the data (using Pagination), the problem is that the returned result, is of type Page which is an interface. I know that the implementation of that interface is a class of type PageImpl but the problem is that when I'm trying to deserialize it, this class doesn't have any default constructor so it fails. I can create a POJO that contain the property needed and that will solve the problem, but I was wondering if there is any spring-trick that allow me to solve this in a better way.

This is an example of my message gateway configuration:

    Map<String, Expression> uriVariableExp = getDefaultEndpointProperties(host, apiKey);
    SpelExpressionParser parser = new SpelExpressionParser();

    uriVariableExp.put("parameter1", parser.parseExpression("payload.parameter1"));
    String endpoint =
            "{host}/resource?parameter1={parameter1}";

    HttpRequestExecutingMessageHandler gateway = new HttpRequestExecutingMessageHandler(endpoint, getRestTemplate());
    gateway.setRequiresReply(true);
    gateway.setHttpMethod(HttpMethod.GET);
    ParameterizedTypeReference<Page<ResourceModel>> typeReference = new ParameterizedTypeReference<Page<ResourceModel>>(){};
    gateway.setExpectedResponseTypeExpression(new ValueExpression<>(typeReference));
    gateway.setUriVariableExpressions(uriVariableExp);
    return gateway;

As you can see, the expectedResponseType is my problem, I tried with the PageImpl but this throw the error of no constructor found.

No, there is no such a built-in trick. Even worse: you client HttpRequestExecutingMessageHandler must not know about the model of the server and not have any Spring Data jars in its classpath. From big height that REST service should not return such a model into the response. Not only your application may be client to that.

Since you have already such a problem you just don't have choice unless introduce some VO POJO to be able to deserialize Page properly, or implement your own HttpMessageConverter to have a hook to instantiate the PageImpl as it is required by the Spring Data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM