【发布时间】:2019-04-30 16:51:11
【问题描述】:
我使用 ErrorDecoder 返回正确的异常而不是 500 状态代码。
有没有办法在解码器中检索原始消息。我可以看到它在 FeignException 内部,但不在 decode 方法中。我只有“状态代码”和一个空的“原因”。
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);
}
}
这里是原始消息:“消息”:“禁止访问文件”
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"}
我还使用我的 FeignClient 接口,如 RestController,因此我不使用任何其他填充了可以封装方法调用的代理的控制器。
@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);
}
【问题讨论】: