Feign ErrorDecoder :检索原始消息

2023-12-25

我使用 ErrorDecoder 返回正确的异常,而不是 500 状态代码。

有没有办法在解码器内检索原始消息。我可以看到它在 FeignException 内部,但不在解码方法中。我所拥有的只是“状态代码”和一个空的“原因”。

public class CustomErrorDecoder implements ErrorDecoder {

    private final ErrorDecoder errorDecoder = new Default();

    @Override
    public Exception decode(String s, Response response) {

        switch (response.status()) {

            case 404:
                return new FileNotFoundException("File no found");
            case 403:
                return new ForbiddenAccessException("Forbidden access");
        }

        return errorDecoder.decode(s, response);
    }
}

这里是原始消息:“message”:“访问文件被禁止”

feign.FeignException: status 403 reading ProxyMicroserviceFiles#getUserRoot(); content:
{"timestamp":"2018-11-28T17:34:05.235+0000","status":403,"error":"Forbidden","message":"Access to the file forbidden","path":"/root"}

另外,我像 RestController 一样使用 FeignClient 接口,因此我不使用任何其他填充有可以封装方法调用的代理的控制器。

   @RestController
   @FeignClient(name = "zuul-server")
   @RibbonClient(name = "microservice-files")

   public interface ProxyMicroserviceFiles {

                @GetMapping(value = "microservice-files/root")
                Object getUserRoot();

                @GetMapping(value = "microservice-files/file/{id}")
                Object getFileById(@PathVariable("id") int id);

    }

如果您想获得响应负载主体,除了Feign例外,就用这个方法:

feignException.contentUTF8();

Example:

    try {
        itemResponse = call(); //method with the feign call
    } catch (FeignException e) {
        logger.error("ResponseBody: " + e.contentUTF8());
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Feign ErrorDecoder :检索原始消息 的相关文章

随机推荐