参考:https://www.jianshu.com/p/d829fd5981b4
参考:https://my.oschina.net/xiaominmin/blog/2986631
ErrorDecoder类
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sany.gcp.infra.feign.common.FeighErrorDecodeException;
import com.sany.gcp.infra.feign.common.FeighErrorResult;
import feign.*;
import feign.codec.ErrorDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import java.io.IOException;
/**
* feigh配置请求拦截器
* @author gw_liuzp 2021/6/9 17:53
*/
@Slf4j
//@Configuration
public class OAuthFeignConfiguration implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
//template.header("Authorization", "Basic ZGhnYXRlOnBwMTIz");
/* ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
Enumeration<String> headerNames = request.getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
String values = request.getHeader(name);
template.header(name, values);
}
}*/
}
@Bean
public ErrorDecoder errorDecoder() {
return new UserErrorDecoder();
}
/**
* 自定义错误
*/
public class UserErrorDecoder implements ErrorDecoder {
@Override
public Exception decode(String methodKey, Response response) {
Exception exception = null;
try {
// 获取原始的返回内容
// String json = Util.toString(response.body().asReader(Charset.defaultCharset()));
String json = Util.toString(response.body().asReader());
// 将返回内容反序列化为Result,这里应根据自身项目作修改
ObjectMapper objectMapper = new ObjectMapper();
// 忽略未知json字段
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
FeighErrorResult result = objectMapper.readValue(json,FeighErrorResult.class);
// 业务异常抛出简单的 RuntimeException,保留原来错误信息
if (!result.getSuccess()) {
exception = new FeighErrorDecodeException(result.getCode(),result.getMessage());
}
} catch (IOException ex) {
log.error(ex.getMessage(), ex);
}
return exception;
}
}
自定义异常类
import lombok.Data;
/**
* feigh调用错误解析器异常类
* @author gw_liuzp 2021/6/10 12:23
*/
@Data
public class FeighErrorDecodeException extends RuntimeException{
private String code;
private String message;
public FeighErrorDecodeException(String code, String message){
super(message);
this.code = code;
this.message = message;
}
}
定义接受json结构对象
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* feigh调用错误信息对象
* @author gw_liuzp 2021/6/10 9:18
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class FeighErrorResult implements Serializable {
// {
// "error":"unauthorized",
// "error_description":"hoth.warn.accountLocked",
// "success":false,
// "code":"hoth.warn.accountLocked",
// "message":"您的账户已锁定,请通过找回密码解锁,或联系平台运维中心"
// }
private String error;
private String error_description;
private Boolean success;
private String code;
private String message;
}
在feign注解配置configuration
@FeignClient(value = "hzero-oauth", path = "/oauth", configuration = OAuthFeignConfiguration.class, fallbackFactory = OAuthServiceFallBack.class)