重新 ErrorDecoder 即可,比如下面例子中在登录鉴权时想使用认证服务器抛出 OAuth2Exception 的异常,代码如下:
import com.fasterxml.jackson.databind.ObjectMapper;
import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import java.io.IOException;
@Configuration
public class FeignErrorDecoder implements ErrorDecoder {
@Override
public Exception decode(String methodKey, Response response) {
try {
String message = Util.toString(response.body().asReader());
ObjectMapper objectMapper = new ObjectMapper();
OAuth2Exception oAuth2Exception = objectMapper.readValue(message, OAuth2Exception.class);
return oAuth2Exception;
} catch (IOException ignored) {
return decode(methodKey, response);