- 反序列化

  1. import com.caucho.hessian.HessianException;
  2. import com.caucho.hessian.io.AbstractDeserializer;
  3. import com.caucho.hessian.io.AbstractHessianInput;
  4. import com.caucho.hessian.io.IOExceptionWrapper;
  5.  
  6. import java.io.IOException;
  7. import java.time.LocalDateTime;
  8. import java.time.ZoneOffset;
  9.  
  10. /**
  11. * @author zenglw
  12. * @date 2018/6/7
  13. */
  14. public class LocalDateTimeDeserializer extends AbstractDeserializer {
  15.  
  16. @Override
  17. public Class getType()
  18. {
  19. return LocalDateTime.class;
  20. }
  21.  
  22. @Override
  23. public Object readObject(AbstractHessianInput in,
  24. Object []fields)
  25. throws IOException
  26. {
  27. String []fieldNames = (String []) fields;
  28.  
  29. int ref = in.addRef(null);
  30.  
  31. long initValue = Long.MIN_VALUE;
  32.  
  33. for (int i = 0; i < fieldNames.length; i++) {
  34. String key = fieldNames[i];
  35.  
  36. if (key.equals("value")) {
  37. initValue = in.readUTCDate();
  38. } else {
  39. in.readObject();
  40. }
  41. }
  42. Object value = create(initValue);
  43. in.setRef(ref, value);
  44. return value;
  45. }
  46.  
  47. private Object create(long initValue)
  48. throws IOException
  49. {
  50. if (initValue == Long.MIN_VALUE) {
  51. throw new IOException(LocalDateTime.class + " expects name.");
  52. }
  53. try {
  54. return LocalDateTime.ofEpochSecond(new Long(initValue)/1000,Integer.valueOf(String.valueOf(initValue%1000))*1000,ZoneOffset.of("+8"));
  55. } catch (Exception e) {
  56. throw new IOExceptionWrapper(e);
  57. }
  58. }
  59. }

- 序列化

  1. import com.caucho.hessian.io.AbstractHessianOutput;
  2. import com.caucho.hessian.io.AbstractSerializer;
  3.  
  4. import java.io.IOException;
  5. import java.time.LocalDateTime;
  6. import java.time.ZoneOffset;
  7.  
  8. /**
  9. * @author zenglw
  10. * @date 2018/6/7
  11. */
  12. public class LocalDateTimeSerializer extends AbstractSerializer {
  13.  
  14. @Override
  15. public void writeObject(Object obj, AbstractHessianOutput out)
  16. throws IOException
  17. {
  18. if (obj == null) {
  19. out.writeNull();
  20. } else {
  21. Class cl = obj.getClass();
  22.  
  23. if (out.addRef(obj)) {
  24. return;
  25. }
  26. // ref 返回-2 便是开始写Map
  27. int ref = out.writeObjectBegin(cl.getName());
  28.  
  29. if (ref < -1) {
  30. out.writeString("value");
  31. Long milliSecond = ((LocalDateTime) obj).toInstant(ZoneOffset.of("+8")).toEpochMilli();
  32. out.writeUTCDate(milliSecond);
  33. out.writeMapEnd();
  34. } else {
  35. if (ref == -1) {
  36. out.writeInt(1);
  37. out.writeString("value");
  38. out.writeObjectBegin(cl.getName());
  39. }
  40.  
  41. Long milliSecond = ((LocalDateTime) obj).toInstant(ZoneOffset.of("+8")).toEpochMilli();
  42. out.writeUTCDate(milliSecond);
  43. }
  44. }
  45. }
  46. }

- hessian的序列化工厂

  1. import com.caucho.hessian.io.ExtSerializerFactory;
  2. import com.caucho.hessian.io.SerializerFactory;
  3. import com.klxx.ta.common.util.hessian.LocalDateTimeDeserializer;
  4. import com.klxx.ta.common.util.hessian.LocalDateTimeSerializer;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7.  
  8. /**
  9. * @author zenglw
  10. * @date 2018/6/7
  11. */
  12. @Configuration
  13. public class HessianConfig {
  14.  
  15. @Bean
  16. public SerializerFactory serializerFactory() {
  17. // DO 自定义hessian反序列化
  18. // step 1. 定义外部序列化工厂
  19. ExtSerializerFactory extSerializerFactory = new ExtSerializerFactory();
  20. extSerializerFactory.addSerializer(java.time.LocalDateTime.class,new LocalDateTimeSerializer());
  21. extSerializerFactory.addDeserializer(java.time.LocalDateTime.class,new LocalDateTimeDeserializer());
  22. // step 2. 序列化工厂
  23. SerializerFactory serializerFactory = new SerializerFactory();
  24. serializerFactory.addFactory(extSerializerFactory);
  25. return serializerFactory;
  26. }
  27. }

- hessian服务端暴露服务

  1. import com.caucho.hessian.io.SerializerFactory;
  2. import com.klxx.ta.foundation.api.OrganizationInfoApi;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.remoting.caucho.HessianServiceExporter;
  7.  
  8. /**
  9. * Hessian注册对外提供服务的service
  10. * @author zenglw
  11. * @date 2018/6/4
  12. */
  13. @Configuration
  14. public class HessianExportConfig {
  15.  
  16. @Autowired
  17. private OrganizationInfoApi organizationInfoApi;
  18.  
  19. @Bean(name = "organizationInfoApi")
  20. public HessianServiceExporter accountService(SerializerFactory serializerFactory) throws Exception {
  21. HessianServiceExporter exporter = new HessianServiceExporter();
  22. exporter.setSerializerFactory(serializerFactory);
  23. exporter.setService(organizationInfoApi);
  24. exporter.setServiceInterface(OrganizationInfoApi.class);
  25. return exporter;
  26. }
  27.  
  28. }

- hessian客户端的服务代理配置

  1. import com.caucho.hessian.io.SerializerFactory;
  2. import com.klxx.ta.foundation.api.OrganizationInfoApi;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.remoting.caucho.HessianProxyFactoryBean;
  7.  
  8. /**
  9. * @author zenglw
  10. * @date 2018/6/7
  11. */
  12. @Configuration
  13. public class HessianProxyConfig {
  14.  
  15. @Value("${hessian.external.service.url.foundation}")
  16. private String foundationUrl;
  17.  
  18. @Bean(name = "accountServiceApi")
  19. public HessianProxyFactoryBean accountServiceApi(SerializerFactory serializerFactory) {
  20. HessianProxyFactoryBean hessianProxyFactoryBean = new HessianProxyFactoryBean();
  21. hessianProxyFactoryBean.setSerializerFactory(serializerFactory);
  22. hessianProxyFactoryBean.setServiceUrl(foundationUrl + "/organizationInfoApi");
  23. hessianProxyFactoryBean.setServiceInterface(OrganizationInfoApi.class);
  24. return hessianProxyFactoryBean;
  25. }
  26. }

Spring boot 集成hessian - LocalDateTime序列化和反序列化的更多相关文章

  1. spring boot 集成 zookeeper 搭建微服务架构

    PRC原理 RPC 远程过程调用(Remote Procedure Call) 一般用来实现部署在不同机器上的系统之间的方法调用,使得程序能够像访问本地系统资源一样,通过网络传输去访问远程系统资源,R ...

  2. SpringBoot系列:Spring Boot集成Spring Cache,使用RedisCache

    前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...

  3. 【spring boot】【redis】spring boot 集成redis的发布订阅机制

    一.简单介绍 1.redis的发布订阅功能,很简单. 消息发布者和消息订阅者互相不认得,也不关心对方有谁. 消息发布者,将消息发送给频道(channel). 然后是由 频道(channel)将消息发送 ...

  4. Lombok安装及Spring Boot集成Lombok

    文章目录 Lombok有什么用 使用Lombok时需要注意的点 Lombok的安装 spring boot集成Lombok Lombok常用注解 @NonNull @Cleanup @Getter/@ ...

  5. (35)Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 本文章牵涉到的技术点比较多:Spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对 ...

  6. Spring Boot 2.X(六):Spring Boot 集成Redis

    Redis 简介 什么是 Redis Redis 是目前使用的非常广泛的免费开源内存数据库,是一个高性能的 key-value 数据库. Redis 与其他 key-value 缓存(如 Memcac ...

  7. Spring Boot 集成 RabbitMQ 实战

    Spring Boot 集成 RabbitMQ 实战 特别说明: 本文主要参考了程序员 DD 的博客文章<Spring Boot中使用RabbitMQ>,在此向原作者表示感谢. Mac 上 ...

  8. SpringBoot(十一): Spring Boot集成Redis

    1.在 pom.xml 中配置相关的 jar 依赖: <!-- 加载 spring boot redis 包 --> <dependency> <groupId>o ...

  9. Spring Boot集成Jasypt安全框架

    Jasypt安全框架提供了Spring的集成,主要是实现 PlaceholderConfigurerSupport类或者其子类. 在Sring 3.1之后,则推荐使用PropertySourcesPl ...

随机推荐

  1. async的基本用法

    1. async函数的基本形式 //函数声明 async function foo() {} //函数表达式 const foo = async function () {}; //对象的方法 let ...

  2. eclipse ide for java ee developers 开发环境搭建(J2EE) 【转载】

    使用eclipse真的有年头了,相信java程序员没有不知道它的,最近在给团队中新来的应届生做指导,专门讲解了一下Eclipse开发环境的搭建过程, 一是帮助他们尽快的熟悉IDE的使用,二也是保证团队 ...

  3. Codeforces Round #331 (Div. 2) B. Wilbur and Array

    B. Wilbur and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. Human life FZU - 2295 最大权闭合子图(第一次遇到被教育了)

    Xzz is playing a MMORPG "human life". In this game, there are N different skills. Some ski ...

  5. POJ 2226 Muddy Fields(二分匹配 巧妙的建图)

    Description Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R ...

  6. simpleDateFormat的 学习

    http://blog.csdn.net/qq_27093465/article/details/53034427

  7. dns随笔(部分转载)

    1.allow-notify allow-notify 定义了一个匹配列表并且只应用于从dns区域(slave zone),比如,这个列表是一个ip列表,它 2. 触发同步的过程 http://www ...

  8. 数据结构:Rope-区间翻转

    BZOJ1269 上一篇文章介绍了Rope的简单应用,这里多了一个操作,区间翻转 同时维护一正一反两个rope……反转即交换两个子串 下面给出代码: #include<cstdio> #i ...

  9. [Luogu 2596] ZJOI2006 书架

    [Luogu 2596] ZJOI2006 书架 第一次指针写 FHQ_Treap(省选噩梦数据结构)AC 啦! 省选试机写它,紧张过度失败了. 省选 Day 1 考场写它,写挂了. 省选 Day 1 ...

  10. C# 从串口读取数据

    最近要做系统集成,需要从串口读取数据,随学习一下相关知识: 以下是从串口读取数据 public static void Main() { SerialPort mySerialPort = new S ...