我正在使用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;
}
答案 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