com.feingto.cloud.config.feign.JacksonDecoder Maven / Gradle / Ivy
package com.feingto.cloud.config.feign;
import com.feingto.cloud.core.json.JSONObjectMapper;
import feign.FeignException;
import feign.Response;
import feign.Util;
import feign.codec.Decoder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Type;
/**
* Jackson 反序列化
*
* @author longfei
*/
public class JacksonDecoder implements Decoder {
private final static JSONObjectMapper mapper = new JSONObjectMapper();
@Override
public Object decode(final Response response, Type type) throws IOException, FeignException {
if (response.status() == 404) return Util.emptyValueOf(type);
if (response.body() == null) return null;
Reader reader = response.body().asReader();
if (!reader.markSupported()) {
reader = new BufferedReader(reader, 1);
}
reader.mark(1);
if (reader.read() == -1) {
return null;
}
reader.reset();
return mapper.readValue(reader, mapper.constructType(type));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy