gpt4 book ai didi

java - Spring Integration 过滤器异常

转载 作者:行者123 更新时间:2023-11-30 07:48:31 27 4
gpt4 key购买 nike

我使用 SpringIntegration-filter 来验证我的 WS 消息。我实现 validator 进行验证,如果 WS 消息有效,它们将返回 true。但如果 WS 消息无效,它们将抛出 MyValidationException。

有没有办法使用 SpringIntegration-filter 来处理这个异常?如果我不返回 false,过滤器将不起作用。

我的代码示例如下。我想在丢弃流程中使用我的验证异常。

@Bean
public IntegrationFlow incomingRequest() {
return f -> f
.<IncomingRequest>filter(message ->
validatorFactory.validator(message.getName())
.validate(message),
filterEndpointSpec ->
filterEndpointSpec.discardChannel(discardChannel()))
.<IncomingRequest>handle((payload, headers) ->
applicationService.handle(payload));
}

@Bean
public IntegrationFlow discard() {
return IntegrationFlows.from(discardChannel())
.log("DISCARD FLOW")
.get();
}

@Bean(name = "discard.input")
public MessageChannel discardChannel() {
return MessageChannels.direct().get();
}

最佳答案

鉴于当您检查 WS 请求时异常来自验证,您必须将调用包围在 try catch 中。如果抛出异常,则捕获异常并返回false,表示验证失败。

@Bean
public IntegrationFlow incomingRequest2() {
return f -> f
.filter(this::isValid, filterEndpointSpec ->
filterEndpointSpec.discardFlow(f2 -> f2.transform(this::getReason))
.discardChannel(discardChannel()))
.<IncomingRequest>handle((payload, headers) ->
applicationService.handle(payload));
}

还有辅助方法。

public boolean isValid(IncomingRequest message) {
try {
return validatorFactory.validator(message.getName())
.validate(message);
} catch (Exception e) { // your exception
return false;
}
}

public String getReason(IncomingRequest message) { // return the object you need
try {
validatorFactory.validator(message.getName())
.validate(message);
return null;
} catch (Exception e) { // process exception as you want
return e.getMessage();
}
}

关于java - Spring Integration 过滤器异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49215428/

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