1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- package com.fdkankan.common.config;
- import com.alibaba.cloud.commons.io.IOUtils;
- import com.alibaba.fastjson.JSONObject;
- import com.fdkankan.common.constant.ServerCode;
- import com.fdkankan.common.exception.BusinessException;
- import com.fdkankan.common.response.ResultData;
- import feign.Response;
- import feign.codec.Decoder;
- import lombok.extern.slf4j.Slf4j;
- import java.io.IOException;
- import java.lang.reflect.Type;
- import java.nio.charset.StandardCharsets;
- import java.util.Objects;
- @Slf4j
- public final class ResultStatusDecoder implements Decoder {
- public static final String CONTENT_KEY = "content";
- final Decoder delegate;
- public ResultStatusDecoder(Decoder delegate) {
- Objects.requireNonNull(delegate, "Decoder must not be null. ");
- this.delegate = delegate;
- }
- @Override
- public Object decode(Response response, Type type) throws IOException {
- // 判断是否返回参数是否是异常
- String resultStr = IOUtils.toString(response.body().asInputStream(), StandardCharsets.UTF_8);
- log.info("feign调用返回,result msg ->{}",resultStr);
- try {
- ResultData resultData = JSONObject.parseObject(resultStr, ResultData.class);
- if(resultData.getCode() != ServerCode.SUCCESS.code()){
- throw new BusinessException(resultData.getCode(),resultData.getMessage());
- }
- }catch (Exception e){
- e.printStackTrace();
- }
- // 回写body
- return delegate.decode(response.toBuilder().body(resultStr, StandardCharsets.UTF_8).build(), type);
- }
- }
|