我有以下简单的代理集成流程。其主要任务是接收来自代理的请求,将其发送到实际端点,获取响应并将其发送回客户端。
@SpringBootApplication
@EnableIntegration
public class IntegrationApp {
@Value("${narko.pin}")
private String pinUrl;
public static void main(String[] args) {
SpringApplication.run(MinzdravApplication.class, args);
}
@Bean
public DirectChannel requestPinChannel() {
return new DirectChannel();
}
@Bean
public DirectChannel replyPinChannel() {
return new DirectChannel();
}
@Bean
public IntegrationFlow httpProxyFlowPin() throws Exception {
return IntegrationFlows
.from(Http.inboundGateway("/narko/api/patient/by-pinpp")
.requestChannel(requestPinChannel()).replyChannel(replyPinChannel())
.mappedRequestHeaders("activityid")
.errorChannel("httpProxyErrorFlow.input")
)
.wireTap(sf->sf.handle(new InwardMessageHandler()))
.enrichHeaders(h -> h.header("Content-Type", "application/json"))
.handle(Http.outboundGateway(pinUrl).charset("utf-8")
.expectedResponseType(String.class))
.channel(replyPinChannel())
.get();
}
@Bean
public IntegrationFlow httpProxyErrorFlow() {
return f -> f
.transform(Throwable::getCause)
.<HttpClientErrorException>handle((p, h) ->
new RuntimeException("custom exception"));
}
}
当出站网关的api关闭时。我有以下错误:
{
"timestamp": "2022-08-10T12:51:58.561+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/narko/api/patient/by-pinpp"
}
我在日志上有以下例外:
java.lang.ClassCastException: class org.springframework.web.client.ResourceAccessException cannot be cast to class org.springframework.web.client.HttpClientErrorException (org.springframework.web.client.ResourceAccessException and org.springframework.web.client.HttpClientErrorException are in unnamed module of loader 'app')
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.integration.handler.LambdaMessageProcessor.processMessage(LambdaMessageProcessor.java:104) ~[spring-integration-core-5.5.10.jar:5.5.10]
at org.springframework.integration.handler.ServiceActivatingHandler.handleRequestMessage(ServiceActivatingHandler.java:105) ~[spring-integration-core-5.5.10.jar:5.5.10]
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:136) ~[spring-integration-core-5.5.10.jar:5.5.10]
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:56) ~[spring-integration-core-5.5.10.jar:5.5.10]
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:115) ~[spring-integration-core-5.5.10.jar:5.5.10]
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:133) ~[spring-integration-core-5.5.10.jar:5.5.10]
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:106) ~[spring-integration-core-5.5.10.jar:5.5.10]
如何创建自定义异常响应?任何导航或提示都将不胜感激。
发布于 2022-08-10 14:27:27
首先,您不需要.replyChannel(replyPinChannel()
和.channel(replyPinChannel())
。入站网关发送带有replyChannel
头的消息,这是流中最后一个应答端点,而不是创建它的outputChannel
,它将与该replyChannel
进行协商。
其次,关于错误处理程序的解决方案是可以的,但是您可以看到自己处在堆栈跟踪中,只是没有转换成正确的类型:ResourceAccessException
不是HttpClientErrorException
的一个实例。请考虑期待一个RestClientException
,这对于ResourceAccessException
和HttpClientErrorException
来说都是超级的。
https://stackoverflow.com/questions/73307102
复制相似问题