我使用 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);
}