张晓波
2023-09-19 164694c47c35d6654df69b533e8dbf8b5423efc5
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
package com.thhy.general.config.feign;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.thhy.general.common.BasicMessage;
import com.thhy.general.common.BasicResult;
import com.thhy.general.exception.BasicException;
import feign.FeignException;
import feign.InvocationContext;
import feign.Response;
import feign.ResponseInterceptor;
import feign.codec.DecodeException;
import feign.codec.Decoder;
 
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.LinkedHashMap;
 
public class FeignResonse implements Decoder {
 
    @Override
    public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
        Response.Body body = response.body();
        InputStream inputStream = body.asInputStream();
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes);
        String result = new String(bytes,"UTF-8");
        System.out.println(new String(bytes,"UTF-8"));
        JSONObject json = JSON.parseObject(result);
        BasicResult basicResult = JSON.toJavaObject(json,BasicResult.class);
        basicResult.setSuccess(json.getBoolean("success"));
        return basicResult;
    }
}