Reputation: 1006
I have followed the link Spring integration: handle http error with oubound gateway
But I don't have full understanding.
I looked at 3. Implementing a ResponseErrorHandler of https://www.baeldung.com/spring-rest-template-error-handling, but not sure what to do at // handle SERVER_ERROR etc
My flow:
Rest end point calls gateway method and then the below.
<int:gateway id="tService"
service-interface="h.lr.eai.TGateway"
default-reply-channel="dest-channel"
default-request-timeout="5000" default-reply-timeout="5000">
<int:method name="vrCorrect" request-channel="tInChannel"/>
<int:method name="...." />
....
</int:gateway>
<int:chain input-channel="tInChannel" output-channel="headerFilterChannel">
<int:header-enricher>
<int:header name="Accept" value="application/json" />
<int:header name="Content-Type" value="application/json" />
</int:header-enricher>
<int-http:outbound-gateway
url="${TURL}/im/rest/something"
http-method="POST"
header-mapper="headerMapper"
error-handler="errorHandler"
expected-response-type="java.lang.String">
</int-http:outbound-gateway>
</int:chain>
<bean id="errorHandler" class="com.util.ErrorHandler" />
The java class. Thought there is not much implementation, it looks like without this, I am not getting the error message thrown by external service.
Is there a way I can get rid of this class, as it is almost default [though added CLIENT and SERVER error check, based on baeldung article?
public class ErrorHandler extends DefaultResponseErrorHandler {
@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
return (
response.getStatusCode().series() == HttpStatus.Series.CLIENT_ERROR
|| response.getStatusCode().series() == HttpStatus.Series.SERVER_ERROR);
}
@Override
public void handleError(ClientHttpResponse response) throws IOException { /* Handle Exceptions */
}
}
I see suggestions to use Advices. can I ask for an example? [not sure if this is spring rest or spring integration]
EDIT 1:
I am calling an external service. It can give an error message like below
{
"code": "F01",
"userMessage": "The is some data error....",
"parameters": {}
}
Before I added error-handler to http-outbound-gateway, I was always getting response as "Bad Request" [and missing a clear message like above]
After adding error-handler and the class, I am able to forward, whatever error message, external service is giving [in its response body]. This is acceptable, but I am not having any implementation in handleError [like you answered]. In such case, is there a way of getting rid of this class and utilize any OOTB class? because I have no justification of why it is there [ may be I don't understand its importance].
P.S. @Artem Bilan, Your answer may suffice [if I need the class for my problem]
Upvotes: 0
Views: 1610
Reputation: 121262
Not clear what is your issue, but I'll try to explain some things.
You definitely might not need that custom ErrorHandler
, because a default one is pretty good and in the end it just does this:
protected void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException {
String statusText = response.getStatusText();
HttpHeaders headers = response.getHeaders();
byte[] body = getResponseBody(response);
Charset charset = getCharset(response);
switch (statusCode.series()) {
case CLIENT_ERROR:
throw HttpClientErrorException.create(statusCode, statusText, headers, body, charset);
case SERVER_ERROR:
throw HttpServerErrorException.create(statusCode, statusText, headers, body, charset);
default:
throw new UnknownHttpStatusCodeException(statusCode.value(), statusText, headers, body, charset);
}
}
So, it parses a response and throws respective exception to the caller.
If you need to react somehow to this or that exception while calling that REST service, you need to consider to use an ExpressionEvaluatingRequestHandlerAdvice
for the <int-http:outbound-gateway>
. This way an exception propagated from the DefaultResponseErrorHandler
can be sent to some failureChannel
for possible logic how to react to this exception.
See more info in the Docs: https://docs.spring.io/spring-integration/docs/current/reference/html/#message-handler-advice-chain
If that is not what you expect, please, rephrase your question because it isn't clear for me yet...
Thanks for understanding.
Upvotes: 2