一、前言
关于使用Feign的feign-master包中通过feign.jackson.JacksonEncoder、feign.jackson.JacksonDecoder类基于jackson进行加密解密,详情参见源码说明部分。
二、源码说明
1.JacksonEncoder加密类
package feign.jackson;@b@@b@import com.fasterxml.jackson.annotation.JsonInclude;@b@import com.fasterxml.jackson.core.JsonProcessingException;@b@import com.fasterxml.jackson.databind.JavaType;@b@import com.fasterxml.jackson.databind.Module;@b@import com.fasterxml.jackson.databind.ObjectMapper;@b@import com.fasterxml.jackson.databind.SerializationFeature;@b@@b@import java.lang.reflect.Type;@b@import java.util.Collections;@b@@b@import feign.RequestTemplate;@b@import feign.codec.EncodeException;@b@import feign.codec.Encoder;@b@@b@public class JacksonEncoder implements Encoder {@b@@b@ private final ObjectMapper mapper;@b@@b@ public JacksonEncoder() {@b@ this(Collections.<Module>emptyList());@b@ }@b@@b@ public JacksonEncoder(Iterable<Module> modules) {@b@ this(new ObjectMapper()@b@ .setSerializationInclusion(JsonInclude.Include.NON_NULL)@b@ .configure(SerializationFeature.INDENT_OUTPUT, true)@b@ .registerModules(modules));@b@ }@b@@b@ public JacksonEncoder(ObjectMapper mapper) {@b@ this.mapper = mapper;@b@ }@b@@b@ @Override@b@ public void encode(Object object, Type bodyType, RequestTemplate template) {@b@ try {@b@ JavaType javaType = mapper.getTypeFactory().constructType(bodyType);@b@ template.body(mapper.writerFor(javaType).writeValueAsString(object));@b@ } catch (JsonProcessingException e) {@b@ throw new EncodeException(e.getMessage(), e);@b@ }@b@ }@b@}
2.JacksonDecoder解密类
package feign.jackson;@b@@b@import com.fasterxml.jackson.databind.DeserializationFeature;@b@import com.fasterxml.jackson.databind.Module;@b@import com.fasterxml.jackson.databind.ObjectMapper;@b@import com.fasterxml.jackson.databind.RuntimeJsonMappingException;@b@@b@import java.io.BufferedReader;@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@public class JacksonDecoder implements Decoder {@b@@b@ private final ObjectMapper mapper;@b@@b@ public JacksonDecoder() {@b@ this(Collections.<Module>emptyList());@b@ }@b@@b@ public JacksonDecoder(Iterable<Module> modules) {@b@ this(new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)@b@ .registerModules(modules));@b@ }@b@@b@ public JacksonDecoder(ObjectMapper mapper) {@b@ this.mapper = mapper;@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@ if (!reader.markSupported()) {@b@ reader = new BufferedReader(reader, 1);@b@ }@b@ try {@b@ // Read the first byte to see if we have any data@b@ reader.mark(1);@b@ if (reader.read() == -1) {@b@ return null; // Eagerly returning null avoids "No content to map due to end-of-input"@b@ }@b@ reader.reset();@b@ return mapper.readValue(reader, mapper.constructType(type));@b@ } catch (RuntimeJsonMappingException e) {@b@ if (e.getCause() != null && e.getCause() instanceof IOException) {@b@ throw IOException.class.cast(e.getCause());@b@ }@b@ throw e;@b@ }@b@ }@b@}