FeignDecoder.java
5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.glsx.admin.config;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson.JSON;
import com.glsx.admin.exception.BusinessException;
import com.glsx.admin.exception.ResultEnum;
import feign.Response;
import feign.codec.Decoder;
import lombok.extern.slf4j.Slf4j;
/**
* <p>Description: feign 自定义解码器
* <p>Project: chejia-smart
* <p>目前实现了 {@link com.glsx.plat.core.web.R} 的解码器 {@link FeignDecoderForR}。</p>
* <p>如果需要定义不同的解码器,则继承此类,并实现此类中所有方法和 {@link FeignParser} 接口。</p>
*
* @param <T> 方法出参类型
* @author wbh
* @date 2021-05-08
*/
@Slf4j
public abstract class FeignDecoder<T> implements Decoder {
@Override
public Object decode(Response response, Type type) {
Object result;
try {
if (response.status() >= 200 && response.status() < 300) {
// 正常返回,解析返回对象
result = this.handleNormalResponse(response, type);
} else {
// 非正常返回,抛出异常
this.handleErrorResponse(response);
result = null;
}
} catch (BusinessException e) {
throw e;
} catch (Exception e) {
throw BusinessException.create(ResultEnum.REMOTE_SERVICE_ERROR);
}
return result;
}
/**
* 处理正常返回,即 HTTP 状态码为 2xx
*
* @param response feign 请求
* @param type 方法出参类型
* @return 结果
* @throws IOException 异常
*/
private Object handleNormalResponse(Response response, Type type) throws IOException {
Object result;
Response.Body body = response.body();
if (body == null) {
result = null;
} else {
String respJson = IoUtil.read(body.asReader(StandardCharsets.UTF_8));
Type responseType = this.getResponseType();
// 响应对象
T resp = JSON.parseObject(respJson, responseType);
// 判断出参类型和响应对象类型是否匹配,即响应对象要能赋值给方法出参
boolean methodTypeMatched = type.getTypeName().equals(responseType.getTypeName());
//boolean methodTypeMatched = (type instanceof Class)
// && responseType.isAssignableFrom((Class<?>) type);
if (methodTypeMatched) {
// 类型匹配:无需进一步解析,直接返回该对象
result = resp;
} else {
// 类型不匹配:开始解析响应对象
this.ensureResponseSuccess(resp);
result = this.parseResponse(resp, type);
}
}
return result;
}
/**
* 处理非正常返回,即 2xx 之外的 HTTP 状态码
*
* @param response feign 请求
* @throws IOException 异常
* @deprecated 貌似捕捉不到 2xx 之外的响应
*/
@Deprecated
private void handleErrorResponse(Response response) throws IOException {
Response.Body body = response.body();
if (body == null) {
throw BusinessException.create(ResultEnum.REMOTE_SERVICE_ERROR);
} else {
String respJson = IoUtil.read(body.asReader(StandardCharsets.UTF_8));
Type responseType = this.getResponseType();
T resp = JSON.parseObject(respJson, responseType);
throw BusinessException.create(this.extractCode(resp), this.extractMessage(resp));
}
}
///**
// * 获取响应对象类型
// *
// * @return 类型
// */
//@SuppressWarnings("unchecked")
//private Class<T> getResponseType() {
// ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
// return (Class<T>) type.getActualTypeArguments()[0];
//}
/**
* 获取响应对象类型
*
* @return 类型
*/
private Type getResponseType() {
ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
return type.getActualTypeArguments()[0];
}
/**
* 解析响应对象中的内容
*
* @param resp 响应对象
* @param type 解析的目标类型
* @return 结果
*/
private Object parseResponse(T resp, Type type) {
Object result = (this.extractData(resp) == null) ? null
: this.getParser(type).parse(resp, type);
return result;
}
/**
* 获取响应对象解析器
*
* @param type 解析的目标类型
* @return 解析器
*/
protected abstract FeignParser<T> getParser(Type type);
/**
* 提取响应对象中的状态码
*
* @param resp 响应对象
* @return 结果
*/
protected abstract int extractCode(T resp);
/**
* 提取响应对象中的信息
*
* @param resp 响应对象
* @return 结果
*/
protected abstract String extractMessage(T resp);
/**
* 提取响应对象中的数据
*
* @param resp 响应对象
* @return 结果
*/
protected abstract Object extractData(T resp);
/**
* 确定响应对象的返回是正常的,非正常则抛出异常
*
* @param resp 响应对象
*/
protected abstract void ensureResponseSuccess(T resp);
/**
* 响应对象解析类,解析成最终需要的方法出参对象
*
* @param <T> 方法出参类型
*/
protected interface FeignParser<T> {
/**
* 解析对象成方法返回对象
*
* @param resp 响应体
* @param type 方法出参类型
* @return 结果
*/
Object parse(T resp, Type type);
}
}