gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:14:21 24 4
gpt4 key购买 nike

我正在使用 Spring Integration 来执行一些 http 请求,然后解析它并最终将其保存到数据库
我想在转换方法中访问 App 对象,正如您所看到的 App 对象是 download 方法的参数(从另一个 channel ),解决方案是什么?
这是我的代码:

@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;
}

最佳答案

如果您与 payload 完全匹配,则可以使用多个参数,其余所有内容都必须通过 @Header 配置。你不能有两个payload ,并且 Java 方法不能有两个返回值。

所以,如果您想携带 App对象与 ResponseEntity<DetailsItemParser[]> 并排考虑将其添加到 headers .

您可以使用HeaderEnricher在发送至 channel1 之前或返回整个Message<?>来自download()方法并添加App使用 MessageBuilder 到标题.

convert()方法则可以指定为:

public App convert(DetailsItemParser payload, @Header("app") App app)

更新

HeaderEnricher通过 Java 配置的示例:

    @Bean
@Transformer(inputChannel = "routingSlipHeaderChannel", outputChannel = "processChannel")
public HeaderEnricher headerEnricher() {
return new HeaderEnricher(Collections.singletonMap(IntegrationMessageHeaderAccessor.ROUTING_SLIP,
new RoutingSlipHeaderValueMessageProcessor(routeStrategy())));
}

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

关于java - Spring 集成在方法之间传递多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46306048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com