- Encoder :编码器,作用于请求阶段,将对象编码到请求体中。
- Decoder:解码器,作用域响应阶段,解析HTTP 响应消息。
默认的编码器它仅能处理String类型、[byte]类型,显然是很难使用的,因为我们经常会在请求参数中放入对象。
解决调用openFeign返回String改成放入对象
自定义OpenFeign编码器
package com.beimao.decoder;
import cn.hutool.core.util.TypeUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.smart.core.exception.BizException;
import feign.FeignException;
import feign.Response;
import feign.Util;
import feign.codec.DecodeException;
import feign.codec.Decoder;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
@Slf4j
public class FeignResponseDecoder implements Decoder{
@Override
public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
try {
//判断响应是否为null,对应着openfeign请求有没有成功
if (response.body() == null) {
throw new DecodeException(response.status(), "没有数据响应", response.request());
}
//将响应的数据转成 字符串
String bodyStr = Util.toString(response.body().asReader(Util.UTF_8));
//再次将响应的数据转换成JSON类型
JSONObject jsonObject = JSONUtil.parseObj(bodyStr, true);
log.warn("openfeign解析消息:{}",jsonObject.get("msg"));
//获取json对象的code值,并判断是否为0,如果不为0抛出异常
int code = (int) jsonObject.get("code");
if (code != 0) {
throw new BizException(code, jsonObject.get("msg").toString());
}
//从json对象中获取data并转成 string类型
String dataStr = jsonObject.get("data").toString();
//获取返回的type的类型(是data的类型)的全路径名(引用类型)
Class<?> aClass = TypeUtil.getClass(type);
Object result = null;
//判断返回类型是否是List类型(因为list类型需要手动转化,List类型中有泛型,Object类型不能转)
if (aClass.isAssignableFrom(List.class)) {
//获取到List泛型中的类型
Type typeArgument = TypeUtil.getTypeArgument(type);
//将Type类型的 typeArgument 转化成Class类型
Class<?> aClass2 = TypeUtil.getClass(typeArgument);
//将data字符串转换 成 List泛型中的类型,并组合成List类型
result = JSONUtil.toList(dataStr, aClass2);
} else {
if (aClass.getName().startsWith("java.lang")) {
// 是Java自带类型
result=jsonObject.get("data");
} else {
// 不是Java自带类型
result = JSONUtil.toBean(dataStr, aClass);
}
}
return result;
} catch (Throwable ex) {
throw new DecodeException(1111, "openfeign解析错误", response.request());
}
}
}
放入容器
package com.beimao.config;
import com.beimao.decoder.FeignResponseDecoder;
import feign.codec.Decoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignConfig {
@Bean
public Decoder feignDecoder() {
return new FeignResponseDecoder();
}
}

692

被折叠的 条评论
为什么被折叠?



