Feign is a Java to HTTP client binder inspired by Retrofit, JAXRS-2.0, and WebSocket. Feign’s first goal was reducing the complexity of binding Denominator uniformly to HTTP APIs regardless of RESTfulness.
Feign exception handling in Spring Cloud
Upasana | December 10, 2019 | 2 min read | 14,648 views
Feign is a declarative web service client for inter-service communicate. It is quite similar to retrofit that is used as a web service client in android.
Exception handling with Feign
OpenFeign’s FeignException doesn’t bind to a specific HTTP status (i.e. doesn’t use Spring’s @ResponseStatus annotation), which makes Spring default to 500 whenever faced with a FeignException.
{
"timestamp": "2019-07-28T08:53:12.168+0000",
"status": 500,
"error": "Internal Server Error",
"message": "status 400 reading client#sendMessage(MessagePayload)",
"path": "/send-notification"
}
That’s because the root cause for FeignException may not be even related to Http status code sent by remote web service.
In order to propagate the actual error message to the client, we can write our custom ExceptionHandler using a ControllerAdvice.
import feign.FeignException;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.Map;
@RestControllerAdvice
public class FeignExceptionHandler {
@ExceptionHandler(FeignException.BadRequest.class) (1)
public Map<String, Object> handleFeignStatusException(FeignException e, HttpServletResponse response) {
response.setStatus(e.status());
return new JSONObject(e.contentUTF8()).toMap(); (2)
}
}
1 | Handling FeignException for BadRequests only. We can write similar ExceptionHandlers for other types of exceptions. |
2 | We are assuming here that remote web service returns a JSON response for error messages |
After we add this ExceptionHandler, we will be able to see custom exception response with additional details from remote web service.
{
"exception": "org.springframework.web.bind.MethodArgumentNotValidException",
"path": "/api/message/notification/",
"error_code": 2151,
"message": "Target id not valid",
"status": 400,
"timestamp": 1564304731547
}
Top articles in this category:
- Feign RequestInterceptor in Spring Boot
- Feign Client Logging and connection timeout
- Java AWS Lambda using Spring Cloud Function
- AWS Lambda in Kotlin using Spring Cloud Function
- Retrofit vs Feign for Server Side
- Spring Boot Sidecar
- Prevent Lost Updates in Database Transaction using Spring Hibernate