如何传递多个动态标头和请求正文以在 Spring 集成中进行休息调用

发布于 2025-01-13 02:12:14 字数 2678 浏览 2 评论 0 原文

MyGateway(在服务接口中定义)是一个接口,将包含一个请求正文(JsonFormat),我将在 http-outbound-gateway 中将其传递给其余调用

<!-- Gateway -->
    <int:gateway id="requestGateway" 
                 service-interface="com.spring.example.MyGateway"
                 default-request-channel="requestChannel12345"/>
    
    <int:channel id="requestChannel12345"/>
    

    
<bean id="methodInsideService"    class="com.spring.example.ServiceImpl"/>

使用下面的 Service-Activator I创建了一个模型类,其中包含标头的值,以便我可以在进行休息调用时使用动态标头 --->下面

<int:service-activator input-channel="requestChannel12345"
output-channel="responseChannel1234567" 
ref="methodInsideService" 
method="methodExample"/>

<bean id="clientHttpRequestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory"/>

这里我接受请求通道(responseChannel1234567)作为动态标头响应的outpt-channel(responseChannel1234567) --->这里我的实际 json 请求被我创建的标头有效负载覆盖,以将其用作动态标头值

<int:chain input-channel="responseChannel1234567" >
     <int:header-enricher>
     <int:header name="header1"  expression="payload.getheader1()" ></int:header>
     <int:header name="header2" expression="payload.getheader2()" ></int:header>
     <int:header name="header3" expression="payload.getheader3()" ></int:header>
     
     </int:header-enricher>
     <int-http:outbound-gateway 
     id="requestGateway" 
     url="http://localhost/test?queryParam1={queryParam1}&amp;queryParam2={queryParam2}&amp;queryParam3={queryParam3}"
     http-method="POST"
     request-factory="clientHttpRequestFactory" 
     header-mapper="headerMapper12345"
     expected-response-type="java.lang.String">
     <int-http:uri-variable name="queryParam1" expression="payload.getqueryParam1()"/>
     <int-http:uri-variable name="queryParam2" expression="payload.getqueryParam2()"/>
     <int-http:uri-variable name="queryParam3" expression="payload.getqueryParam3()"/>
     </int-http:outbound-gateway>
     
     </int:chain>
    <bean id="headerMapper12345" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
     <property name="outboundHeaderNames" value="HTTP_REQUEST_HEADERS, header1,header2,header3" />
     <property name="userDefinedHeaderPrefix" value="" /></bean>
     
    
     
</beans>

现在我的问题是如何传递多个标头(动态将在代码级别传递),多个查询参数(在代码级别动态)和请求正文(JSON 格式)以对其他服务进行休息调用。 我们可以从接口传递请求正文,但如何从接口或使用服务激活器传递标头和查询参数,然后相应地配置 xml 以对其他休息端点进行休息 http 出站网关调用。 提前致谢

MyGateway(defined in service-interface) is a Interface will contain one request body(JsonFormat) which I will be passing it in http-outbound-gateway for rest call

<!-- Gateway -->
    <int:gateway id="requestGateway" 
                 service-interface="com.spring.example.MyGateway"
                 default-request-channel="requestChannel12345"/>
    
    <int:channel id="requestChannel12345"/>
    

    
<bean id="methodInsideService"    class="com.spring.example.ServiceImpl"/>

Using below Service-Activator I created one model class which will contains values of headers so that I can use dynamic headers while doing a rest call ---> below

<int:service-activator input-channel="requestChannel12345"
output-channel="responseChannel1234567" 
ref="methodInsideService" 
method="methodExample"/>

<bean id="clientHttpRequestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory"/>

Here I am accepting request channel(responseChannel1234567) as outpt-channel(responseChannel1234567) of dynamic headers response ---> here my actual json request overwritten with headers payload which I am creating to use it as dynamic header values

<int:chain input-channel="responseChannel1234567" >
     <int:header-enricher>
     <int:header name="header1"  expression="payload.getheader1()" ></int:header>
     <int:header name="header2" expression="payload.getheader2()" ></int:header>
     <int:header name="header3" expression="payload.getheader3()" ></int:header>
     
     </int:header-enricher>
     <int-http:outbound-gateway 
     id="requestGateway" 
     url="http://localhost/test?queryParam1={queryParam1}&queryParam2={queryParam2}&queryParam3={queryParam3}"
     http-method="POST"
     request-factory="clientHttpRequestFactory" 
     header-mapper="headerMapper12345"
     expected-response-type="java.lang.String">
     <int-http:uri-variable name="queryParam1" expression="payload.getqueryParam1()"/>
     <int-http:uri-variable name="queryParam2" expression="payload.getqueryParam2()"/>
     <int-http:uri-variable name="queryParam3" expression="payload.getqueryParam3()"/>
     </int-http:outbound-gateway>
     
     </int:chain>
    <bean id="headerMapper12345" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
     <property name="outboundHeaderNames" value="HTTP_REQUEST_HEADERS, header1,header2,header3" />
     <property name="userDefinedHeaderPrefix" value="" /></bean>
     
    
     
</beans>

Now My Question is how to pass multiple headers(dynamic will pass at code level),multiple query-params(dynamic at code level) and request body(JSON Format) to make a rest call to other services.
Request body we can pass from Interface, but how to pass the headers and query params from interface or using service-activator and then configure xml accordingly to make a rest http outbound gateway call to other rest endpoint.
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一紙繁鸢 2025-01-20 02:12:14

问题不清楚,需要一些细节来揭示您到底在做什么以及您的期望是什么。

默认情况下,请求正文是从消息payload映射的。
底层的 RestTemplate 将使用其 HttpMessageConverter 将此类正文序列化为网络上的相应数据表示形式。

标头根据以下规则从消息映射到 HTTP 请求:

https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http-header-mapping

所以,如果你有一些发送到此 HTTP 通道之前的标头集适配器,您可以在mapped-request-headers中指定它们的名称。或者只使用 * 映射所有标头。

可以使用 refmethod 配置 来填充标头的动态映射:

    <xsd:attribute name="ref" type="xsd:string">
        <xsd:annotation>
            <xsd:documentation>
                Reference to an Object to be invoked for header values.
                The 'method' attribute is required
                along
                with this.
            </xsd:documentation>
            <xsd:appinfo>
                <tool:annotation kind="ref"/>
            </xsd:appinfo>
        </xsd:annotation>
    </xsd:attribute>
    <xsd:attribute name="method" type="xsd:string">
        <xsd:annotation>
            <xsd:documentation>
                Method to be invoked on the referenced Object as specified by the
                'ref' attribute. The method
                should return a Map with String-typed keys.
            </xsd:documentation>
            <xsd:appinfo>
                <tool:annotation>
                    <tool:expected-method type-ref="@ref"/>
                </tool:annotation>
            </xsd:appinfo>
        </xsd:annotation>
    </xsd:attribute>

https://docs.spring.io/spring-integration/docs/current/reference/html/message-transformation.html#header-enricher

The question is not clear and requires some details to reveal what exactly are you doing and what are your expectations.

The request body is mapped from the message payload by default.
The underlying RestTemplate is going to use its HttpMessageConverters to serialize such a body to respective data presentation over the network.

The headers are mapped from the message to the HTTP request according these rules:

https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http-header-mapping

So, if you have some set of headers before sending to this HTTP channel adapter, you can specify their names in the mapped-request-headers. Or just use * to map all the headers.

The <header-enricher> can be configured with the ref and method to populate something dynamic map of headers:

    <xsd:attribute name="ref" type="xsd:string">
        <xsd:annotation>
            <xsd:documentation>
                Reference to an Object to be invoked for header values.
                The 'method' attribute is required
                along
                with this.
            </xsd:documentation>
            <xsd:appinfo>
                <tool:annotation kind="ref"/>
            </xsd:appinfo>
        </xsd:annotation>
    </xsd:attribute>
    <xsd:attribute name="method" type="xsd:string">
        <xsd:annotation>
            <xsd:documentation>
                Method to be invoked on the referenced Object as specified by the
                'ref' attribute. The method
                should return a Map with String-typed keys.
            </xsd:documentation>
            <xsd:appinfo>
                <tool:annotation>
                    <tool:expected-method type-ref="@ref"/>
                </tool:annotation>
            </xsd:appinfo>
        </xsd:annotation>
    </xsd:attribute>

https://docs.spring.io/spring-integration/docs/current/reference/html/message-transformation.html#header-enricher

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文