赞
踩
根据日常业务需求微服务之间数据传输对象可能是比较复杂的对象,就以Mybatis-plus QueryWrapper为例,对象中的属性可能存在抽象类的引用,甚至是集合中的抽象类,如果使用json进行传输时,json可能无法转化为对象
以QueryWrapper为例,normal变量中3个元素的真实类型是不相同的,但他们都实现了接口ISqlSegment
所以当我们反序列化json字符串成对象的时候,无法知道数组中的三个ISqlSegment对象具体的实现具体是什么,所以会抛出转化异常
可以看出 json序列化的方式并没有办法在反序列化时知道到抽象类实例的具体子类型,所以博主采用了jdk的序列化的方式传输(借鉴rabbitMq SimpleMessageConverter 消息传输的思路)
这里引用一个博主的文章代码,本文以feign为例
Feign完美解决服务之间传递文件、传递list,map、对象等情况 - sprouting的个人空间 - OSCHINA - 中文开源技术交流社区
在编码对象的时候我们判断如果需要使用jdk编码传输,则使用jdk编码传输,否则默认使用json(上面文章是使用json编码)
- package cn.ssq.config;
-
-
- import cn.ssq.http.model.JdkSerializable;
- import cn.ssq.uploadPolicyService.entity.OnlineShop;
- import com.alibaba.fastjson.JSON;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.dataformat.xml.XmlMapper;
- import feign.RequestTemplate;
- import feign.codec.EncodeException;
- import feign.codec.Encoder;
- import org.springframework.core.io.InputStreamResource;
- import org.springframework.core.io.Resource;
- import org.springframework.http.HttpEntity;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.HttpOutputMessage;
- import org.springframework.http.MediaType;
- import org.springframework.http.converter.HttpMessageConverter;
- import org.springframework.util.LinkedMultiValueMap;
- import org.springframework.util.SerializationUtils;
- import org.springframework.web.client.RestTemplate;
- import org.springframework.web.multipart.MultipartFile;
-
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.lang.reflect.Type;
- import java.nio.charset.Charset;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
-
-
- /**
- * @author :LX
- * 创建时间: 2020/10/14. 15:06
- * 地点:广州
- * 目的: 自定义表单编码器。feign 实现多pojo传输与MultipartFile上传 编码器,需配合开启feign自带注解使用
- * 用于支持多对象和文件的上传
- *
- * Encoder的原理就是将每个参数json序列化,设置requestHeader为Multipart/form-data,采用表单请求去请求生成者提供的接口。
- * 这个方法能够同时发送多个实体文件,以及MultipartFile[]的数组.
- *
- * 参考资料:
- * https://github.com/pcan/feign-client-test
- * 备注说明:
- */
- public class FeignSpringFormEncoder implements Encoder{
-
- private final List<HttpMessageConverter<?>> converters = new RestTemplate().getMessageConverters();
-
- public static final Charset UTF_8 = Charset.forName("UTF-8");
-
- public FeignSpringFormEncoder() {}
-
- /**
- * 实现一个 HttpOutputMessage
- */
- private class HttpOutputMessageImpl implements HttpOutputMessage{
- /**
- * 输出流,请求体
- */
- private final OutputStream body;
- /**
- * 请求头
- */
- private final HttpHeaders headers;
-
- public HttpOutputMessageImpl(OutputStream body, HttpHeaders headers) {
- this.body = body;
- this.headers = headers;
- }
-
- @Override
- public OutputStream getBody() throws IOException {
- return body;
- }
-
- @Override
- public HttpHeaders getHeaders() {
- return headers;
- }
- }
-
- /**
- * 判断是否表单请求
- * @param type
- * @return
- */
- static boolean isFormRequest(Type type){
- return MAP_STRING_WILDCARD.equals(type);
- }
-
- /**
- * 内部静态类,保存 MultipartFile 数据
- */
- static class MultipartFileResource extends InputStreamResource {
- /**
- * 文件名
- */
- private final String filename;
- /**
- * 文件大小
- */
- private final long size;
-
- /**
- * 构造方法
- * @param inputStream
- * @param filename
- * @param size
- */
- public MultipartFileResource(InputStream inputStream, String filename, long size) {
- super(inputStream);
- this.filename = filename;
- this.size = size;
- }
-
- @Override
- public String getFilename() {
- return this.filename;
- }
-
- @Override
- public InputStream getInputStream() throws IOException, IllegalStateException {
- return super.getInputStream();
- }
-
- @Override
- public long contentLength(){
- return size;
- }
- }
-
- /**
- * 重写编码器
- * @param object
- * @param bodyType
- * @param template
- * @throws EncodeException
- */
- @Override
- public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
- if (isFormRequest(bodyType)){
- final HttpHeaders multipartHeaders = new HttpHeaders();
- multipartHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
- encodeMultipartFormRequest((Map<Object, ?>) object, multipartHeaders, template);
- } else {
- final HttpHeaders jsonHeaders = new HttpHeaders();
- jsonHeaders.setContentType(MediaType.APPLICATION_JSON);
- encodeRequest(object, jsonHeaders, template);
- }
- }
-
- /**
- * 对有文件、表单的进行编码
- * @param formMap
- * @param multipartHeaders
- * @param template
- */
- private void encodeMultipartFormRequest(Map<Object, ?> formMap, HttpHeaders multipartHeaders, RequestTemplate template){
- if (formMap == null){
- throw new EncodeException("无法对格式为null的请求进行编码。");
- }
-
- LinkedMultiValueMap<Object, Object> map = new LinkedMultiValueMap<>();
- //对每个参数进行检查校验
- for (Entry<Object, ?> entry : formMap.entrySet()){
- Object value = entry.getValue();
- //不同的数据类型进行不同的编码逻辑处理
- if (isMultipartFile(value)){
- //单个文件
- map.add(entry.getKey(), encodeMultipartFile((MultipartFile)value));
-
- } else if (isMultipartFileArray(value)){
- //多个文件
- encodeMultipartFiles(map, (String) entry.getKey(), Arrays.asList((MultipartFile[]) value));
-
- } else {
- //普通请求数据
- map.add(entry.getKey(), serializeObject(value));
- }
- }
-
- encodeRequest(map, multipartHeaders, template);
- }
-
- /**
- * 对请求进行编码
- * @param value
- * @param requestHeaders
- * @param template
- */
- private void encodeRequest(Object value, HttpHeaders requestHeaders, RequestTemplate template){
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- HttpOutputMessage dummyRequest = new HttpOutputMessageImpl(outputStream, requestHeaders);
-
- try {
- Class<?> requestType = value.getClass();
- MediaType requestContentType = requestHeaders.getContentType();
- for (HttpMessageConverter<?> messageConverter : converters){
- if (messageConverter.canWrite(requestType, requestContentType)){
- ((HttpMessageConverter<Object>) messageConverter).write(value, requestContentType, dummyRequest);
- break;
- }
- }
- } catch (IOException e) {
- throw new EncodeException("无法对请求进行编码:", e);
- }
-
- HttpHeaders headers = dummyRequest.getHeaders();
- if (headers != null){
- for (Entry<String, List<String>> entry : headers.entrySet()){
- template.header(entry.getKey(), entry.getValue());
- }
- }
-
- /*
- 请使用模板输出流。。。如果文件太大,这将导致问题,因为整个请求都将在内存中。
- */
- template.body(outputStream.toByteArray(), UTF_8);
- }
-
- /**
- * 序列化对象
- * @param obj
- * @return
- */
- private HttpEntity<?> serializeObject(Object obj){
- HttpHeaders jsonPartHeaders = new HttpHeaders();
- Object content;
- if (obj instanceof JdkSerializable){
- //需要jdk则使用jdk编码
- jsonPartHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
- content = SerializationUtils.serialize(obj);
- }else {
- //否则默认使用json
- if (obj instanceof String){
- content = obj;
- }else {
- jsonPartHeaders.setContentType(MediaType.APPLICATION_JSON);
- content = JSON.toJSONString(obj);
- }
- }
-
- return new HttpEntity<>(content, jsonPartHeaders);
- }
-
- /**
- * 编码MultipartFile文件,将其转换为HttpEntity,同时设置 Content-type 为 application/octet-stream
- * @param map 当前请求 map.
- * @param name 数组字段的名称
- * @param fileList 要处理的文件
- */
- private void encodeMultipartFiles(LinkedMultiValueMap<Object, Object> map, String name, List<? extends MultipartFile> fileList){
- HttpHeaders filePartHeaders = new HttpHeaders();
- //设置 Content-type
- filePartHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
- try {
- for (MultipartFile file : fileList){
- Resource multipartFileResource = new MultipartFileResource(file.getInputStream(), file.getOriginalFilename(), file.getSize());
- map.add(name, new HttpEntity<>(multipartFileResource, filePartHeaders));
- }
- } catch (IOException e) {
- throw new EncodeException("无法对请求进行编码:", e);
- }
- }
-
- /**
- * 编码MultipartFile文件,将其转换为HttpEntity,同时设置 Content-type 为 application/octet-stream
- * @param file 要编码的文件
- * @return
- */
- private HttpEntity<?> encodeMultipartFile(MultipartFile file){
- HttpHeaders filePartHeaders = new HttpHeaders();
- //设置 Content-type
- filePartHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
- try {
- Resource multipartFileResource = new MultipartFileResource(file.getInputStream(), file.getOriginalFilename(), file.getSize());
- return new HttpEntity<>(multipartFileResource, filePartHeaders);
- } catch (IOException e) {
- throw new EncodeException("无法对请求进行编码:", e);
- }
- }
-
- /**
- * 判断是否多个 MultipartFile
- * @param object
- * @return
- */
- private boolean isMultipartFileArray(Object object){
- return object != null && object.getClass().isArray() && MultipartFile.class.isAssignableFrom(object.getClass().getComponentType());
- }
-
- /**
- * 判断是否MultipartFile文件
- * @param object 要判断的对象
- * @return
- */
- private boolean isMultipartFile(Object object){
- return object instanceof MultipartFile;
- }
-
-
- }
具体实现代码段
- /**
- * 序列化对象
- * @param obj
- * @return
- */
- private HttpEntity<?> serializeObject(Object obj){
- HttpHeaders jsonPartHeaders = new HttpHeaders();
- Object content;
- if (obj instanceof JdkSerializable){
- //需要jdk则使用jdk编码
- jsonPartHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
- content = SerializationUtils.serialize(obj);
- }else {
- //否则默认使用json
- if (obj instanceof String){
- content = obj;
- }else {
- jsonPartHeaders.setContentType(MediaType.APPLICATION_JSON);
- content = JSON.toJSONString(obj);
- }
- }
-
- return new HttpEntity<>(content, jsonPartHeaders);
- }
- package cn.ssq.http.model;
-
- import java.io.Serializable;
-
- /**
- * jdk序列化对象,用于标记需要jdk序列化传输的对象
- * @author liufuhao
- * @date 2021/10/05
- */
- public interface JdkSerializable extends Serializable {
- }
我们需要重新封装QueryWrapper的DTO对象并实现自定义的JdkSerializable接口
- package cn.ssq.dto;
-
- import cn.ssq.http.model.JdkSerializable;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-
- /**
- * mybatis-plus查询条件传输类
- */
- public class QueryWrapperDTO<T> extends QueryWrapper<T> implements JdkSerializable {
-
- }
springmvc默认没有jdk反序列化对象转化实现,所以我们需要自己去实现jdk的反序列化
- package cn.ssq.http.converter;
-
- import cn.ssq.http.model.JdkSerializable;
- import org.springframework.amqp.utils.SerializationUtils;
- import org.springframework.http.HttpInputMessage;
- import org.springframework.http.HttpOutputMessage;
- import org.springframework.http.MediaType;
- import org.springframework.http.converter.AbstractHttpMessageConverter;
- import org.springframework.http.converter.HttpMessageNotReadableException;
- import org.springframework.remoting.rmi.CodebaseAwareObjectInputStream;
- import org.springframework.util.ClassUtils;
- import org.springframework.util.StreamUtils;
-
- import java.io.*;
- import java.util.LinkedHashSet;
- import java.util.Properties;
- import java.util.Set;
-
- /**
- * jdk序列化转化器
- * @author liufuhao
- * @date 2021/10/05
- */
- public class JdkSerializableHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
- private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
- private final Set<String> whiteListPatterns = new LinkedHashSet();
-
-
- public JdkSerializableHttpMessageConverter() {
- super(new MediaType[]{new MediaType("application", "octet-stream"), MediaType.ALL});
- }
-
- /**
- * 判断是否支持转化
- * @param clazz
- * @return
- */
- public boolean supports(Class<?> clazz) {
- Class<?>[] interfaces = clazz.getInterfaces();
- for (Class<?> anInterface : interfaces) {
- if (anInterface.equals(JdkSerializable.class)){
- return true;
- }
- }
- return false;
- }
-
- @Override
- public Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
- long contentLength = inputMessage.getHeaders().getContentLength();
- ByteArrayOutputStream bos = new ByteArrayOutputStream(contentLength >= 0L ? (int)contentLength : 4096);
- StreamUtils.copy(inputMessage.getBody(), bos);
- //使用jdk序列化,需要使用jdk反序列化,原理:读取class池中class对象的并构造实例对象
- Object deserialize = SerializationUtils.deserialize(this.createObjectInputStream(new ByteArrayInputStream(bos.toByteArray()), null));
- return deserialize;
- }
-
- @Override
- protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException {
- OutputStream os = outputMessage.getBody();
- Properties properties = new Properties();
- properties.store(os, String.valueOf(SerializationUtils.serialize(obj)));
- }
-
-
- /**
- * 获取构建对象流
- * @param is
- * @param codebaseUrl
- * @return
- * @throws IOException
- */
- protected ObjectInputStream createObjectInputStream(InputStream is, String codebaseUrl) throws IOException {
- return new CodebaseAwareObjectInputStream(is, this.beanClassLoader, codebaseUrl) {
- //根据路径解析获取class对象
- protected Class<?> resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException {
- Class<?> clazz = super.resolveClass(classDesc);
- return clazz;
- }
- };
- }
-
- }
- package cn.ssq.http.config;
-
- import cn.ssq.http.converter.JdkSerializableHttpMessageConverter;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.http.converter.HttpMessageConverter;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
-
- import java.util.List;
-
- /**
- * mvc配置
- * @author liufuhao
- * @date 2021/10/05
- */
- @Configuration
- public class MyWebConfig implements WebMvcConfigurer {
- /**
- * 新增反序列化解析器,解析微服务参数
- * @param converters
- */
- @Override
- public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
-
- converters.add(new JdkSerializableHttpMessageConverter());
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。