未验证 提交 4be701d9 编写于 作者: G Guide哥 提交者: GitHub

Merge pull request #58 from KevinJavaLee/master

add Hessian序列化方法
......@@ -25,6 +25,8 @@
<slf4j.version>1.7.25</slf4j.version>
<!-- protostuff -->
<protostuff.version>1.7.2</protostuff.version>
<!--hessian-->
<hessian.version>4.0.65</hessian.version>
</properties>
<modules>
<module>rpc-framework-simple</module>
......
......@@ -52,5 +52,10 @@
<artifactId>protostuff-runtime</artifactId>
<version>${protostuff.version}</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>${hessian.version}</version>
</dependency>
</dependencies>
</project>
package github.javaguide.serialize.hessian;
import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import github.javaguide.exception.SerializeException;
import github.javaguide.serialize.Serializer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
/**
* Hessian is a dynamically-typed, binary serialization and Web Services protocol designed for object-oriented transmission.
*
* @author Vinlee Xiao
* @createTime 2022/2/23 21:11
*/
public class HessianSerializer implements Serializer {
@Override
public byte[] serialize(Object obj) {
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
HessianOutput hessianOutput = new HessianOutput(byteArrayOutputStream);
hessianOutput.writeObject(obj);
return byteArrayOutputStream.toByteArray();
} catch (Exception e) {
throw new SerializeException("Serialization failed");
}
}
@Override
public <T> T deserialize(byte[] bytes, Class<T> clazz) {
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes)) {
HessianInput hessianInput = new HessianInput(byteArrayInputStream);
Object o = hessianInput.readObject();
return clazz.cast(o);
} catch (Exception e) {
throw new SerializeException("Deserialization failed");
}
}
}
package github.javaguide.serialize.hessian;
import github.javaguide.remoting.dto.RpcRequest;
import org.junit.jupiter.api.Test;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
class HessianSerializerTest {
@Test
public void hessianSerializerTest() {
RpcRequest target = RpcRequest.builder().methodName("hello")
.parameters(new Object[]{"sayhelooloo", "sayhelooloosayhelooloo"})
.interfaceName("github.javaguide.HelloService")
.paramTypes(new Class<?>[]{String.class, String.class})
.requestId(UUID.randomUUID().toString())
.group("group1")
.version("version1")
.build();
HessianSerializer hessianSerializer = new HessianSerializer();
byte[] bytes = hessianSerializer.serialize(target);
RpcRequest actual = hessianSerializer.deserialize(bytes, RpcRequest.class);
assertEquals(target.getGroup(), actual.getGroup());
assertEquals(target.getVersion(), actual.getVersion());
assertEquals(target.getRequestId(), actual.getRequestId());
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册