从IntegrationFlows

时间:2016-02-12 08:27:48

标签: spring-integration

当从extractFramesFlow()传递ObjectNode并到达httpCallbackFlow()时,HTTP请求成功执行,并且JSON格式的有效负载被发送到" call_back" uri 指定。

 @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:无法写入请求:找不到合适的HttpMessageConverter请求类型[com.fasterxml.jackson.databind.node.ObjectNode]和内容类型[application / x-java-serialized-宾语]         在org.springframework.web.client.RestTemplate $ HttpEntityRequestCallback.doWithRequest(RestTemplate.java:811)         在org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:594)         在org.springframework.web.client.RestTemplate.execute(RestTemplate.java:572)         在org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:493)         在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 个答案:

答案 0 :(得分:1)

错误流上的消息没有contentType标题。

这是MessagingException有效负载的错误消息;它有2个属性; causefailedMessage

据推测,主流消息中包含内容类型。您可以使用标题扩充器设置内容类型,或添加

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

在现有错误处理程序之前,从失败邮件中恢复标头。