簡體   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