简体   繁体   English

将HttpRequestExecutingMessageHandler的ExpectedResponseType设置为Page

[英]Set the HttpRequestExecutingMessageHandler's expectedResponseType to a Page

I'm using a MessagingGateway to perform HTTP calls to another service. 我正在使用MessagingGateway执行对另一个服务的HTTP调用。 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. 该端点返回数据切片(使用分页),问题在于返回的结果的类型为Page (接口)。 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. 我知道该接口的实现是PageImpl类型的类,但是问题是当我尝试反序列化该类时,该类没有任何默认构造函数,因此它失败。 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. 我可以创建一个包含所需属性的POJO,它将解决问题,但是我想知道是否有任何弹簧技巧可以使我以更好的方式解决此问题。

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. 如您所见,expectedResponseType是我的问题,我尝试使用PageImpl但这会引发未找到构造函数的错误。

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. 更糟糕的是:您的客户端HttpRequestExecutingMessageHandler必须不了解服务器的模型,并且其类路径中不得包含任何Spring Data jar。 From big height that REST service should not return such a model into the response. 从高处来看,REST服务不应将这样的模型返回到响应中。 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. 因为您已经遇到了这样的问题,否则您别无选择,除非引入一些VO POJO以便能够正确地反序列化Page ,或者实现您自己的HttpMessageConverter来具有一个钩子来实例化Spring Data所需的PageImpl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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