2016-02-12 128 views
0

當ObjectNode從extractFramesFlow(通過ObjectNode)併到達httpCallbackFlow(),成功地執行HTTP請求和JSON格式淨荷「POST'ed到‘call_back’URI指定。奇怪的行爲返回時從IntegrationFlows

@Bean 
public IntegrationFlow extractFramesFlow() { 
    return IntegrationFlows.from(extractFramesChannel()) 
      .handle(ObjectNode.class, (payload, headers) -> { 
     payload = validateFields(payload); 
     String path = payload.get("path").asText(); 
     try { 
      File moviePath = new File(path); 
      ArrayNode arrayNode = mapper.createArrayNode(); 
      String imageType = payload.path("image_type").asText("JPG"); 
      String prefix = payload.path("prefix").asText(); 
      Tools.thumbnails(moviePath, payload.get("slice").asInt(), payload.get("scale").asInt(), 
        imageType, prefix, file -> arrayNode.add(file.toString())); 
      payload.set("files", arrayNode); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return payload; 
    }).enrichHeaders(h-> h.header("errorChannel", "asyncErrorChannel", true)) 
      .<ObjectNode, Boolean>route(p-> !p.hasNonNull("id"), 
      m->m.channelMapping("true","httpCallbackFlow.input") 
        .channelMapping("false","uploadToS3Channel")).get(); 
} 

@Bean 
public IntegrationFlow httpCallbackFlow() { 
    return f->f.handle(Http.<JsonNode>outboundChannelAdapter(m->m.getPayload().get("call_back").asText())); 
} 

然而,當ObjectNode從handleAsyncErrors鏈()流動併到達同一httpCallbackFlow(),我們得到這是由

org.springframework.web.client.RestClientException引起一個異常:無法寫入請求:在org.springframework.web.client.RestTemplate找不到適合請求類型[com.fasterxml.jackson.databind.node.ObjectNode]和內容類型[application/x-java-serialized-object] 的HttpMessageConverter $ HttpEntityRequestCallback.doWithRequest(RestTemplate.java:811) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:594) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:572) at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:493) at org.springframework.integration.http。 outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:382) ...... 24多個

@Bean 
public IntegrationFlow handleAsyncErrors() { 
    return IntegrationFlows.from(asyncErrorChannel()) 
      .<MessagingException>handle((p, h) -> { 
       ObjectNode objectNode = mapper.createObjectNode(); 
       objectNode.put("call_back", "http://some.test.uri"); 
       return objectNode; 
      }).channel("httpCallbackFlow.input").get(); 
} 

我不知道爲什麼我們得到這個異常由完全相同的IntegrationFlow儘管處理。

回答

1

錯誤流中的消息沒有contentType標頭。

這是一個帶有MessagingException有效載荷的錯誤消息;其中有2個屬性; causefailedMessage

大概你在主流信息上有內容類型。你可以用頭濃縮塔設置的內容類型,或者現有的錯誤處理程序之前添加

.<MessagingException, Message<?>>transform(p -> p.getFailedMessage()) 

,從失敗的消息恢復的頭。

+0

謝謝你的迴應,你是對的。 IntegrationFlow接受來自http客戶端的POST請求,並且其contentType頭部值設置爲「application/json」,正如您已經指出的那樣。該消息異步處理,結果將被髮送回在JSON請求負載中定義的「call_back」URL。以這種方式重新使用標題可能不是一個好習慣。 – hanishi

+0

是的;最好使用頭richter(overwrite = true)明確設置contentType,而不是依賴某個上游組件設置(比如從入站請求繼承它)。 –