0

我对 Spring Integration 有一点了解,到目前为止我已经使用了 JMS 和 File 出站适配器,现在我想介绍用于 Spring REST 支持的 HTTP 出站适配器。到目前为止一切顺利,我能够调用外部 REST api,但存在以下任何问题。

Spring 集成配置

<int-http:outbound-gateway id="outbound.gateway"
    request-channel="get.request.channel" url="https://jsonplaceholder.typicode.com/posts/1"
    http-method="GET" expected-response-type="com.cst.entity.Post"
    charset="UTF-8" reply-timeout="5000" reply-channel="reply.channel">
</int-http:outbound-gateway>

出站网关调用

public void restTest() throws Exception{

    Message<?> message = MessageBuilder.withPayload().build();
    getRequestChannel.send(message);
    Message<?> receivedMsg = receivedChannel.receive();
    Post post = (Post) receivedMsg.getPayload();

    System.out.println("############## ServerMsg ##############");
    System.out.println(post.toString());
    System.out.println("############## Done! ##############");

}

但是我想开发一个框架,开发人员可以使用任何方法调用任何 REST url 并期望不同类型的响应类型。我找到了一种通过引入如下动态设置 URL 的方法

<int-http:outbound-gateway id="outbound.gateway"
    request-channel="get.request.channel" url="{fhirurl}"
    http-method="GET" expected-response-type="com.bst.pages.crm.web.Post"
    charset="UTF-8" reply-timeout="5000" reply-channel="reply.channel">
</int-http:outbound-gateway>

public void restTest() throws Exception{

    FHIRInput input = new FHIRInput();
    input.setUrl(url);

    Message<?> message = MessageBuilder.withPayload(input).build();
    getRequestChannel.send(message);
    Message<?> receivedMsg = receivedChannel.receive();
    Post post = (Post) receivedMsg.getPayload();

    System.out.println("############## ServerMsg ##############");
    System.out.println(post.toString());
    System.out.println("############## Done! ##############");

}

然后我尝试使用上述方法实现具有动态响应类型的动态 HTTP 方法,但它不起作用,看起来我们只能使用<int-http:uri-variable/>.

什么是理想的解决方案。感谢你的帮助

谢谢,凯思

编辑

在遵循以下评论之后,我能够实现一个框架,开发人员可以在其中根据有效负载内容调用使用动态 URL。下面是我对 HTTP 出站适配器的配置。

<int-http:outbound-gateway id="outbound.gateway"
    request-channel="get.request.channel" url="{fhirurl}"
    http-method-expression="payload.getHttpMethod()" expected-response-type-expression="payload.getResponseType()"
    charset="UTF-8" reply-timeout="5000" reply-channel="reply.channel">

    <int-http:uri-variable name="fhirurl" expression="payload.getUrl()"/>

</int-http:outbound-gateway>

但是我仍在寻找一种将动态请求正文作为 POST 参数传递的方法。由于我们使用有效负载来携带 URL、http 方法和预期的响应类型,我无法传递请求正文。

4

1 回答 1

1

不清楚你的意思是什么

然后我尝试使用上述方法实现具有动态响应类型的动态 HTTP 方法,但它不起作用,看起来我们只能使用<int-http:uri-variable/>.

要处理多种响应类型,请将它们作为字符串 (JSON) 获取,然后使用转换器转换为类型。

编辑1

响应类型和方法可以是表达式:

<xsd:attribute name="expected-response-type-expression" type="xsd:string">
    <xsd:annotation>
        <xsd:documentation>
            SpEL expression to determine the type for the expected response to which the response body should be converted
            The returned value of the expression could be an instance of java.lang.Class or
            java.lang.String representing a fully qualified class name.
            This attribute cannot be provided if expected-response-type has a value
        </xsd:documentation>
    </xsd:annotation>
</xsd:attribute>

<xsd:attribute name="http-method-expression" type="xsd:string">
    <xsd:annotation>
        <xsd:documentation>
            The SpEL expression to determine HTTP method, use when executing requests with this adapter,
            dynamically. This attribute cannot be provided if http-method has a value.
        </xsd:documentation>
    </xsd:annotation>
</xsd:attribute>

解决方案

我能够为一个简单的框架提供一个解决方案,我们可以允许开发人员使用不同的 HTTP 方法和响应类型调用不同的 rest URL。

这是 Spring Integration 的配置

<int:channel id='reply.channel'>
    <int:queue capacity='10' />
</int:channel>
<int:channel id='request.channel'/>

<int:channel id='outbound.Channel'/>

<int:gateway id="outboundGateway"
    service-interface="com.bst.pm.PostGateway"
    default-request-channel="outbound.Channel">
</int:gateway>

<int:object-to-json-transformer input-channel="outbound.Channel" output-channel="request.channel"/>



<int-http:outbound-gateway id="outbound.gateway"
    request-channel="request.channel" url-expression="headers.bstUrl"
    http-method-expression="headers.bstHttpMethod" expected-response-type-expression="headers.bstExpectedResponseType"
    charset="UTF-8" reply-timeout="5000" reply-channel="reply.channel"
    mapped-request-headers="bst*, HTTP_REQUEST_HEADERS">

</int-http:outbound-gateway>

这是我用于调用上述集成系统的 Java 代码。

@Autowired @Qualifier("reply.channel") PollableChannel receivedChannel;
@Autowired @Qualifier("request.channel") MessageChannel getRequestChannel;
@Autowired @Qualifier("outbound.Channel") MessageChannel httpOutboundGateway;

    Post post = new Post();
    post.setTitle("Spring INtegration Test");
    post.setBody("This is a sample request body to test Spring Integration HTTP Outbound gateway");
    post.setUserId(Long.valueOf(1));

    Message<?> message = MessageBuilder.withPayload(post)
                        .setHeader("bstUrl", "https://jsonplaceholder.typicode.com/posts")
                        .setHeader("bstHttpMethod", "POST")
                        .setHeader("bstExpectedResponseType", "com.bst.pages.crm.web.Post")
                         .build();

    httpOutboundGateway.send(message);
    Message<?> receivedMsg = receivedChannel.receive();

    Post post = (Post) receivedMsg.getPayload();
    System.out.println("############## ServerMsg ##############");
    System.out.println(o);
    System.out.println("############## Done! ##############");
于 2018-05-30T13:30:13.460 回答