提交 ca60d913 编写于 作者: chenxiang@lab.ibiz5.com's avatar chenxiang@lab.ibiz5.com

Feign Page对象解析

上级 8ed0a3ff
package cn.ibizlab.util.client;
import com.alibaba.fastjson.util.ParameterizedTypeImpl;
import com.google.common.collect.Lists;
import feign.FeignException;
import feign.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
@Slf4j
@Component
public class FeignPageDecoder extends SpringDecoder {
public FeignPageDecoder(ObjectFactory<HttpMessageConverters> messageConverters) {
super(messageConverters);
}
@Override
public Object decode(final Response response, Type type)
throws IOException, FeignException {
if (type instanceof ParameterizedType && ((ParameterizedType) type).getRawType() == Page.class) {
try {
((ParameterizedType) type).getActualTypeArguments()[0].getClass();
ParameterizedTypeImpl ss = new ParameterizedTypeImpl(((ParameterizedType) type).getActualTypeArguments(), null, List.class);
Object ret = super.decode(response, ss);
return new PageImpl((List) ret,
PageRequest.of(
Integer.parseInt(response.headers().get("x-page").toArray()[0].toString()),
Integer.parseInt(response.headers().get("x-per-page").toArray()[0].toString()))
, Long.parseLong(response.headers().get("x-total").toArray()[0].toString()));
} catch (Exception e) {
log.error("FeignDecode转换Page发生错误:" + e.getMessage());
return new PageImpl(Lists.newArrayList(), PageRequest.of(0, 0), 0);
}
}
return super.decode(response, type);
}
}
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册