一、前言
关于使用Feign的feign-master包中通过feign.gson.GsonEncoder、feign.gson.GsonDecoder、feign.gson.GsonFactory类对GSON格式数据进行加密解密,详情参见源码说明部分。
二、源码说明
1.GsonEncoder加密类
package feign.gson;@b@@b@import com.google.gson.Gson;@b@import com.google.gson.TypeAdapter;@b@@b@import java.lang.reflect.Type;@b@import java.util.Collections;@b@@b@import feign.RequestTemplate;@b@import feign.codec.Encoder;@b@@b@public class GsonEncoder implements Encoder {@b@@b@ private final Gson gson;@b@@b@ public GsonEncoder(Iterable<TypeAdapter<?>> adapters) {@b@ this(GsonFactory.create(adapters));@b@ }@b@@b@ public GsonEncoder() {@b@ this(Collections.<TypeAdapter<?>>emptyList());@b@ }@b@@b@ public GsonEncoder(Gson gson) {@b@ this.gson = gson;@b@ }@b@@b@ @Override@b@ public void encode(Object object, Type bodyType, RequestTemplate template) {@b@ template.body(gson.toJson(object, bodyType));@b@ }@b@}
2.GsonDecoder解密类
package feign.gson;@b@@b@import com.google.gson.Gson;@b@import com.google.gson.JsonIOException;@b@import com.google.gson.TypeAdapter;@b@@b@import java.io.IOException;@b@import java.io.Reader;@b@import java.lang.reflect.Type;@b@import java.util.Collections;@b@@b@import feign.Response;@b@import feign.Util;@b@import feign.codec.Decoder;@b@@b@import static feign.Util.ensureClosed;@b@@b@public class GsonDecoder implements Decoder {@b@@b@ private final Gson gson;@b@@b@ public GsonDecoder(Iterable<TypeAdapter<?>> adapters) {@b@ this(GsonFactory.create(adapters));@b@ }@b@@b@ public GsonDecoder() {@b@ this(Collections.<TypeAdapter<?>>emptyList());@b@ }@b@@b@ public GsonDecoder(Gson gson) {@b@ this.gson = gson;@b@ }@b@@b@ @Override@b@ public Object decode(Response response, Type type) throws IOException {@b@ if (response.status() == 404) return Util.emptyValueOf(type);@b@ if (response.body() == null) return null;@b@ Reader reader = response.body().asReader();@b@ try {@b@ return gson.fromJson(reader, type);@b@ } catch (JsonIOException e) {@b@ if (e.getCause() != null && e.getCause() instanceof IOException) {@b@ throw IOException.class.cast(e.getCause());@b@ }@b@ throw e;@b@ } finally {@b@ ensureClosed(reader);@b@ }@b@ }@b@}
3.GsonFactory库类
package feign.gson;@b@@b@import com.google.gson.Gson;@b@import com.google.gson.GsonBuilder;@b@import com.google.gson.TypeAdapter;@b@import com.google.gson.reflect.TypeToken;@b@@b@import java.lang.reflect.Type;@b@import java.util.Map;@b@@b@import static feign.Util.resolveLastTypeParameter;@b@@b@final class GsonFactory {@b@@b@ private GsonFactory() {@b@ }@b@@b@ /**@b@ * Registers type adapters by implicit type. Adds one to read numbers in a {@code Map<String,@b@ * Object>} as Integers.@b@ */@b@ static Gson create(Iterable<TypeAdapter<?>> adapters) {@b@ GsonBuilder builder = new GsonBuilder().setPrettyPrinting();@b@ builder.registerTypeAdapter(new TypeToken<Map<String, Object>>() {@b@ }.getType(), new DoubleToIntMapTypeAdapter());@b@ for (TypeAdapter<?> adapter : adapters) {@b@ Type type = resolveLastTypeParameter(adapter.getClass(), TypeAdapter.class);@b@ builder.registerTypeAdapter(type, adapter);@b@ }@b@ return builder.create();@b@ }@b@}