gpt4 book ai didi

spring-cloud-feign - Feign ErrorDecoder : retrieve the original message

转载 作者:行者123 更新时间:2023-12-04 11:10:09 26 4
gpt4 key购买 nike

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

此外,我像 RestController 一样使用我的 FeignClient 接口(interface),所以我不使用任何其他填充有可以封装方法调用的代理的 Controler。
   @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);

}

最佳答案

这里有一个解决方案,消息实际上作为流在响应正文中。

package com.clientui.exceptions;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.CharStreams;
import feign.Response;
import feign.codec.ErrorDecoder;
import lombok.*;

import java.io.*;

public class CustomErrorDecoder implements ErrorDecoder {

private final ErrorDecoder errorDecoder = new Default();

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

String message = null;
Reader reader = null;

try {
reader = response.body().asReader();
//Easy way to read the stream and get a String object
String result = CharStreams.toString(reader);
//use a Jackson ObjectMapper to convert the Json String into a
//Pojo
ObjectMapper mapper = new ObjectMapper();
//just in case you missed an attribute in the Pojo
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
//init the Pojo
ExceptionMessage exceptionMessage = mapper.readValue(result,
ExceptionMessage.class);

message = exceptionMessage.message;

} catch (IOException e) {

e.printStackTrace();
}finally {

//It is the responsibility of the caller to close the stream.
try {

if (reader != null)
reader.close();

} catch (IOException e) {
e.printStackTrace();
}
}

switch (response.status()) {

case 404:
return new FileNotFoundException(message == null ? "File no found" :
message);
case 403:
return new ForbiddenAccessException(message == null ? "Forbidden
access" : message);

}

return errorDecoder.decode(s, response);
}

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public static class ExceptionMessage{

private String timestamp;
private int status;
private String error;
private String message;
private String path;

}
}

关于spring-cloud-feign - Feign ErrorDecoder : retrieve the original message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53525228/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com