[英]spring integration jsonToObjecttransformer exception
[英]Spring Integration Filter With Exception
我使用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請求時來自validate的異常,您必須將調用包含在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();
}
}
丟棄通道僅獲取被拒絕的入站消息。 無法在過濾器中對其進行更改。
你可以做這樣的事情...
.handle() // return an Exception on validation failure
.filter(...) // filter if payload is exception; the exceptions go to the discard channel
即,將驗證和過濾問題分開。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.