【问题标题】:Spring Integration Http Inbound Gateway Sending blank json replySpring集成Http入站网关发送空白json回复
【发布时间】:2014-10-28 21:44:22
【问题描述】:

以下代码使用 Spring 集成 2.2.6

我想通过回复通道将响应发送回入站网关。我正在从服务激活器发送回复。但是 Spring 无法设置响应而是返回空消息

Spring-Integration xml 配置

<int-http:inbound-gateway id="entryHttpInboundGateway"
        request-channel="entryInputChannel" path="/service/getSE/{uuid}" reply-channel="entryInboundReplyChannel"
        request-payload-type="com.test.ServiceEvent" mapped-response-headers="eventUUID, HTTP_RESPONSE_HEADERS"
        supported-methods="GET, POST"  reply-timeout="1000" view-name="/process/plain2">
        <int-http:header name="eventUUID" expression="#pathVariables.uuid"/>
 </int-http:inbound-gateway>

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" p:order="1" 
        p:defaultContentType="application/json">
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
            </list>
        </property>
</bean>

<int:service-activator id="sv2" input-channel="entryInputChannel" output-channel="outputChannel"
    method="sendReply" requires-reply="true" ref="checkService">
  </int:service-activator>

 <bean id="checkService" class="com.test.CheckService" />

将消息发布到入站适配器回复通道 (com.test.CheckService.java)

// Preparing the message to be sent to reply channel
     InboundReplyResponse inboundReplyResponse = new InboundReplyResponse();
        inboundReplyResponse.setMessage("Success");
        inboundReplyResponse.setEventUUID("12345");

    Map<String, Object> responseHeaderMap = new HashMap<String, Object>();
                    responseHeaderMap.put("eventUUID",eventUUID);
                    responseHeaderMap.put("Content-Type","application/json");

    GenericMessage<InboundReplyResponse> message = new GenericMessage<InboundReplyResponse>(inboundReplyResponse,responseHeaderMap);


    //Reply queue -> entryInboundReplyChannel
        MessageChannel entryInboundReplyChannel =  (MessageChannel) AppFactory.getApplicationContext().getBean("entryInboundReplyChannel");

    entryInboundReplyChannel.send(message);

web.xml

 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-integration-context.xml</param-value>
    </context-param>


    <servlet>
        <servlet-name>ssTesting</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

  <servlet-mapping>    
        <servlet-name>ssTesting</servlet-name>    
        <url-pattern>/service/*</url-pattern>  
    </servlet-mapping>

响应返回给客户

<Empty JSON content>

【问题讨论】:

    标签: java spring-integration


    【解决方案1】:

    已解决

    // Instead of creating a new header, Request Message header should be used  
    
    public  Message<ServiceMsg> (Message<?> inMessage){
    ....
    ....
    GenericMessage<InboundReplyResponse> message = new GenericMessage<InboundReplyResponse>(inboundReplyResponse,inMessage.getHeaders());
    

    【讨论】:

      【解决方案2】:

      您的代码看起来很糟糕。

      您可以直接发送到entryInboundReplyChannel,前提是您在网关的调用线程中。因为网关的sendAndReceive 依赖于MessageHeaders 中的TemporaryReplyChannel。最后一个与inbound-gateway 上的显式reply-channel 无关。

      为什么不将entryInboundReplyChannel 用作output-channel&lt;service-activator&gt;,而只是从sendReply 方法返回消息,而仅仅依靠Framewrok 能力来正确地调节流?

      我的意思是不要手动使用entryInboundReplyChannel.send(message)

      【讨论】:

      • 感谢 Artem 的回复。我想在收到请求时立即使用“uuid”通过回复通道回复客户。我不想等待我的整个处理完成然后发回回复。因此使用 entryInboundReplyChannel.send(message) 发回回复。正如您所提到的使用 出站通道作为回复通道,出站通道被配置为路由器的输入以进行进一步处理。因此,我不得不在具有请求通道输入通道的服务激活器中手动编写发送代码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多