繁体   English   中英

Spring Integration Http入站网关发送空白json回复

[英]Spring Integration Http Inbound Gateway Sending blank json reply

下面的代码使用Spring Integration 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>

您的代码看起来很糟糕。

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

为什么不只将entryInboundReplyChannel用作<service-activator> output-channel ,而只从sendReply方法返回消息,而仅依靠Framewrok的能力正确地进行流传输呢?

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

解决了

// 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());

暂无
暂无

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

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