- 反序列化

import com.caucho.hessian.HessianException;
import com.caucho.hessian.io.AbstractDeserializer;
import com.caucho.hessian.io.AbstractHessianInput;
import com.caucho.hessian.io.IOExceptionWrapper; import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset; /**
* @author zenglw
* @date 2018/6/7
*/
public class LocalDateTimeDeserializer extends AbstractDeserializer { @Override
public Class getType()
{
return LocalDateTime.class;
} @Override
public Object readObject(AbstractHessianInput in,
Object []fields)
throws IOException
{
String []fieldNames = (String []) fields; int ref = in.addRef(null); long initValue = Long.MIN_VALUE; for (int i = 0; i < fieldNames.length; i++) {
String key = fieldNames[i]; if (key.equals("value")) {
initValue = in.readUTCDate();
} else {
in.readObject();
}
}
Object value = create(initValue);
in.setRef(ref, value);
return value;
} private Object create(long initValue)
throws IOException
{
if (initValue == Long.MIN_VALUE) {
throw new IOException(LocalDateTime.class + " expects name.");
}
try {
return LocalDateTime.ofEpochSecond(new Long(initValue)/1000,Integer.valueOf(String.valueOf(initValue%1000))*1000,ZoneOffset.of("+8"));
} catch (Exception e) {
throw new IOExceptionWrapper(e);
}
}
}

- 序列化

import com.caucho.hessian.io.AbstractHessianOutput;
import com.caucho.hessian.io.AbstractSerializer; import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset; /**
* @author zenglw
* @date 2018/6/7
*/
public class LocalDateTimeSerializer extends AbstractSerializer { @Override
public void writeObject(Object obj, AbstractHessianOutput out)
throws IOException
{
if (obj == null) {
out.writeNull();
} else {
Class cl = obj.getClass(); if (out.addRef(obj)) {
return;
}
// ref 返回-2 便是开始写Map
int ref = out.writeObjectBegin(cl.getName()); if (ref < -1) {
out.writeString("value");
Long milliSecond = ((LocalDateTime) obj).toInstant(ZoneOffset.of("+8")).toEpochMilli();
out.writeUTCDate(milliSecond);
out.writeMapEnd();
} else {
if (ref == -1) {
out.writeInt(1);
out.writeString("value");
out.writeObjectBegin(cl.getName());
} Long milliSecond = ((LocalDateTime) obj).toInstant(ZoneOffset.of("+8")).toEpochMilli();
out.writeUTCDate(milliSecond);
}
}
}
}

- hessian的序列化工厂

import com.caucho.hessian.io.ExtSerializerFactory;
import com.caucho.hessian.io.SerializerFactory;
import com.klxx.ta.common.util.hessian.LocalDateTimeDeserializer;
import com.klxx.ta.common.util.hessian.LocalDateTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author zenglw
* @date 2018/6/7
*/
@Configuration
public class HessianConfig { @Bean
public SerializerFactory serializerFactory() {
// DO 自定义hessian反序列化
// step 1. 定义外部序列化工厂
ExtSerializerFactory extSerializerFactory = new ExtSerializerFactory();
extSerializerFactory.addSerializer(java.time.LocalDateTime.class,new LocalDateTimeSerializer());
extSerializerFactory.addDeserializer(java.time.LocalDateTime.class,new LocalDateTimeDeserializer());
// step 2. 序列化工厂
SerializerFactory serializerFactory = new SerializerFactory();
serializerFactory.addFactory(extSerializerFactory);
return serializerFactory;
}
}

- hessian服务端暴露服务

import com.caucho.hessian.io.SerializerFactory;
import com.klxx.ta.foundation.api.OrganizationInfoApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.caucho.HessianServiceExporter; /**
* Hessian注册对外提供服务的service
* @author zenglw
* @date 2018/6/4
*/
@Configuration
public class HessianExportConfig { @Autowired
private OrganizationInfoApi organizationInfoApi; @Bean(name = "organizationInfoApi")
public HessianServiceExporter accountService(SerializerFactory serializerFactory) throws Exception {
HessianServiceExporter exporter = new HessianServiceExporter();
exporter.setSerializerFactory(serializerFactory);
exporter.setService(organizationInfoApi);
exporter.setServiceInterface(OrganizationInfoApi.class);
return exporter;
} }

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

import com.caucho.hessian.io.SerializerFactory;
import com.klxx.ta.foundation.api.OrganizationInfoApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.caucho.HessianProxyFactoryBean; /**
* @author zenglw
* @date 2018/6/7
*/
@Configuration
public class HessianProxyConfig { @Value("${hessian.external.service.url.foundation}")
private String foundationUrl; @Bean(name = "accountServiceApi")
public HessianProxyFactoryBean accountServiceApi(SerializerFactory serializerFactory) {
HessianProxyFactoryBean hessianProxyFactoryBean = new HessianProxyFactoryBean();
hessianProxyFactoryBean.setSerializerFactory(serializerFactory);
hessianProxyFactoryBean.setServiceUrl(foundationUrl + "/organizationInfoApi");
hessianProxyFactoryBean.setServiceInterface(OrganizationInfoApi.class);
return hessianProxyFactoryBean;
}
}

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. UVA.136 Ugly Numbers (优先队列)

    UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...

  2. Kindle 电子书相关的工具软件【转】

    这里是与 Kindle 电子书相关的工具软件.它们可以帮助我们解决在日常使用电子书时所可能遇到的问题,比如 kindle 管理工具.kindle 转换工具.kindle电子书制作工具.kindle 推 ...

  3. css命名冲突解决方法

    css的命名冲突目前有几种解决方法: 1.命名约定 人为的制定一下命名规则以避免冲突,例如前缀,嵌套等 2.CSS in JS 在JavaScript中写CSS,使用工具编译为css,最常见的是sty ...

  4. poco普通线程

    #include "Poco/Thread.h" #include "Poco/RunnableAdapter.h" #include <iostream ...

  5. ACE_DEBUG介绍及日志输出

    转载于:http://blog.csdn.net/zzjxiaozi/article/details/6642925 ACE_DEBUG  常规的一些输出消息   ACE_ERROR  会提供程序出错 ...

  6. liunx系统下安装mysql数据库5.7.13版本

    一:在/usr/local目录下解压安装包

  7. Base class does not contain a constructor that takes '0' argument

    刚刚在写一段直播室网站中的一段程序遇,突然遇到一个错误,如下 'TVLLKBLL.BaseClass' does not contain a constructor that takes 0 argu ...

  8. Spring 源码学习(1) —— 自定义标签

    Spring 工作流程是先加载解析xml配置文件:配置文件中存在默认的标签,也可以自定义标签.解析默认标签调用: private void parseDefaultElement(Element el ...

  9. Android中禁止SlidingPaneLayout的侧滑功能

    Android中使用android.support.v4.widget.SlidingPaneLayout实现侧滑功能的时候,可能出现滑动屏幕时与SlidingPaneLayout的侧滑发生冲突,查看 ...

  10. 任何用户密码都能以sysdba角色登入

    这是因为在安装Oracle的时候默认是使用了操作系统验证: 数据库用sysdba登录的验证有两种方式,一种是通过os认证,一种是通过密码文件验证:登录方式有两种,一种是在数据库主机直接登录(用os认证 ...