在Feig.Builder类中装配的encoder与decoder分别用于编码与解码。
public abstract class Feign {
...
public static class Builder {
private Encoder encoder = new Encoder.Default();
private Decoder decoder = new Decoder.Default();
...
}
}
Encoder是一个接口,在这个接口中已经有一个Default的默认实现,这个接口中最重要的方法就是encode,以提供一个对内容的编码实现.
public interface Encoder {
Type MAP_STRING_WILDCARD = Util.MAP_STRING_WILDCARD;
void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException;
class Default implements Encoder {
@Override
public void encode(Object object, Type bodyType, RequestTemplate template) {
if (bodyType == String.class) {
template.body(object.toString()); //一个字符串的编码器
} else if (bodyType == byte[].class) {
template.body((byte[]) object, null);
} else if (object != null) {
throw new EncodeException(
format("%s is not a type supported by this encoder.", object.getClass()));
}
}
}
}
Decoder与Encoder正好是一个反向执行过程,它提供解码的实现. 它也提供一个默认实现,默认是一个字符串的解码器。
public interface Decoder {
Object decode(Response response, Type type) throws IOException, DecodeException, FeignException;
//默认是一个字符串的解码器
public class Default extends StringDecoder {
@Override
public Object decode(Response response, Type type) throws IOException {
if (response.status() == 404 || response.status() == 204)
return Util.emptyValueOf(type);
if (response.body() == null)
return null;
if (byte[].class.equals(type)) {
return Util.toByteArray(response.body().asInputStream());
}
return super.decode(response, type);
}
}
}
前面我们也讲过,我们可以给Feign加入其它的编码器,通过以下代码完成: decoder(new GsonDecoder())
GitHub2_javax github = Feign.builder()
.decoder(new GsonDecoder())
.options( new Request.Options(1000,3500)) //options方法指定连接超时时长及响应超时时长
.retryer( new Retryer.Default(5000,5000,3)) // retryer方法主要是指定重试策略
.contract(new JAXRSContract()) // Contract 指明是哪种注解规范
.target(GitHub2_javax.class, "https://api.github.com"); // 为构造器配置本地的代理接口,和远程的根目录。代理接口类的每一个接口方法前@RequestLine 声明的值,最终都会加上这个根目录。
下面我们查看一下 GsonDecoder()的实现:
public class GsonDecoder implements Decoder {
private final Gson gson;
public GsonDecoder(Iterable<TypeAdapter<?>> adapters) {
this(GsonFactory.create(adapters));
}
public GsonDecoder() {
this(Collections.<TypeAdapter<?>>emptyList());
}
public GsonDecoder(Gson gson) {
this.gson = gson;
}
@Override
public Object decode(Response response, Type type) throws IOException {
if (response.body() == null)
return null;
Reader reader = response.body().asReader();
try {
return gson.fromJson(reader, type);
} catch (JsonIOException e) {
if (e.getCause() != null && e.getCause() instanceof IOException) {
throw IOException.class.cast(e.getCause());
}
throw e;
} finally {
ensureClosed(reader);
}
}
}
从这些代码中我们可知,GsonDecoder实现了Decoder接口,重写了decode()方法, 在这个方法中,它是调用了google的gson.fromJson()来完成json字符串转为对象的操作的.
以上明白后,其它的Decoder和Encoder就可以自己去看源码学习了.