kpc*_*der 12 exception spring-cloud-feign feign
我有一些 fiegn 客户端发送请求其他微服务。
@FeignClient(name="userservice")
public interface UserClient {
@RequestMapping(
method= RequestMethod.GET,
path = "/userlist")
String getUserByid(@RequestParam(value ="id") String id);
}
Run Code Online (Sandbox Code Playgroud)
现在我正在发送这样的请求
try {
String responseData = userClient.getUserByid(id);
return responseData;
}
catch(FeignException e)
{
logger.error("Failed to get user", id);
}
catch (Exception e)
{
logger.error("Failed to get user", id);
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是,如果发生任何 FeignException,我不会收到任何错误代码。
我需要在其他 APIS 中发送相应的错误代码以发送给调用方
那么如何提取错误代码呢?我想提取错误代码并构建一个 responseEntity
我得到了这段代码,但不知道如何在我的函数中使用。
小智 9
不是同一个问题,但这对我的情况有所帮助。OpenFeign 的 FeignException 不绑定到特定的 HTTP 状态(即不使用 Spring 的 @ResponseStatus 注释),这使得 Spring 在遇到 FeignException 时默认为 500。没关系,因为 FeignException 可能有许多与特定 HTTP 状态无关的原因。
但是,您可以更改 Spring 处理 FeignExceptions 的方式。只需定义一个处理 FeignException 的 ExceptionHandler
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(FeignException.class)
public String handleFeignStatusException(FeignException e, HttpServletResponse response) {
response.setStatus(e.status());
return "feignError";
}
}
Run Code Online (Sandbox Code Playgroud)
此示例使 Spring 返回您收到的相同 HTTP 状态
您是否尝试在您的假客户端上实现 FallbackFactory ?
在 create 方法上,在返回之前,您可以使用以下代码片段检索 http 状态代码:
String httpStatus = cause instanceof FeignException ? Integer.toString(((FeignException) cause).status()) : "";
Run Code Online (Sandbox Code Playgroud)
示例:
String httpStatus = cause instanceof FeignException ? Integer.toString(((FeignException) cause).status()) : "";
Run Code Online (Sandbox Code Playgroud)
我迟到了,但这是我的 2 美分。我们有相同的用例来处理基于错误代码的异常,我们使用custom ErrorDecoder.
public class CustomErrorDecoder implements ErrorDecoder {
@Override
public Exception decode(String methodKey, Response response) {
String requestUrl = response.request().url();
Response.Body responseBody = response.body();
HttpStatus responseStatus = HttpStatus.valueOf(response.status());
if (responseStatus.is5xxServerError()) {
return new RestApiServerException(requestUrl, responseBody);
} else if (responseStatus.is4xxClientError()) {
return new RestApiClientException(requestUrl, responseBody);
} else {
return new Exception("Generic exception");
}
}
}
Run Code Online (Sandbox Code Playgroud)
@Bean在FeignClientConfiguration课堂上返回上述课程。
public class MyFeignClientConfiguration {
@Bean
public ErrorDecoder errorDecoder() {
return new CustomErrorDecoder();
}
}
Run Code Online (Sandbox Code Playgroud)
将此用作 FeignClient 的配置类。
@FeignClient(value = "myFeignClient", configuration = MyFeignClientConfiguration.class)
Run Code Online (Sandbox Code Playgroud)
然后您可以使用GlobalExceptionHandler.
我也有点晚了,但我想确保您也检查其他潜在的解决方案。
Feign with Spring 处理异常的方式是出了名的糟糕,所以我想出了一个自定义解决方案,它创建了这个强大的环境,以您想要的方式定义业务异常。
它利用ErrorDecoder注册到 Feign 客户端的自定义,并增加了根据方法或类级别自定义异常处理的可能性。
| 归档时间: |
|
| 查看次数: |
52768 次 |
| 最近记录: |