Spring集成在方法之间传递多个参数

时间:2017-09-19 17:01:20

标签: java spring spring-integration

我正在使用Spring Integration来做一些http请求,然后解析它最后保存到数据库中 我想在转换方法中访问App对象,因为您可以看到App对象是download方法的参数(从另一个通道调用),解决方案是什么?
这是我的代码:

@ServiceActivator(inputChannel = "channel1", outputChannel = "channel2")
public ResponseEntity<DetailsItemParser[]> download(App app)
{
    String url = config.getAppDetailUrl();
    try
    {
        SSLUtil.turnOffSslChecking();
    }
    catch (Exception e)
    {
        LOG.error(e.getMessage());
    }
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
    HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
    detailsCrawlerRestTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    ResponseEntity<DetailsItemParser[]> responseEntity = detailsCrawlerRestTemplate.exchange(url, HttpMethod.GET, requestEntity, DetailsItemParser[].class, app.getExternalId());

    return responseEntity;

}

@Splitter(inputChannel = "channel2", outputChannel = "channel3")
public List<DetailsItemParser> scrape(ResponseEntity<DetailsItemParser[]> payload)
{
    return new ArrayList<>(Arrays.asList(payload.getBody()));
}



@Filter(inputChannel = "channel3", outputChannel = "channel4")
public boolean filter(DetailsItemParser detailsItemParser)
{
    final Set<ConstraintViolation<DetailsItemParser>> violations = validator.validate(detailsItemParser);
    if (violations != null && !violations.isEmpty())
    {
        return false;
    }

    return true;
}

@Transformer(inputChannel = "channel4", outputChannel = "channel5")
public App convert(DetailsItemParser payload)
{

    App app = appRepository.findById(payload.getId());

    LOG.info("entry={}", o.toString());
    return null;
}

1 个答案:

答案 0 :(得分:1)

如果您与payload完全匹配,则可以使用多个参数,其余部分必须通过@Header进行配置。您不能拥有两个payload,并且您无法从Java方法中获得两次返回。

因此,如果您想将App对象与ResponseEntity<DetailsItemParser[]>一起考虑将其添加到headers

您可以在发送至HeaderEnricher之前使用channel1或从Message<?>方法返回整个download(),并使用{{1}将App添加到标头中}}

然后可以将MessageBuilder方法指定为:

convert()

<强>更新

通过Java Config的public App convert(DetailsItemParser payload, @Header("app") App app) 示例:

HeaderEnricher

参考手册中的更多信息:https://docs.spring.io/spring-integration/reference/html/messaging-transformation-chapter.html#_configuring_a_header_enricher_with_java_configuration