netty(二)–使用Marshalling编解码java对象

本文介绍了如何在Netty中利用Marshalling进行Java对象的序列化和反序列化,详细阐述了从添加Maven依赖到定义服务端和客户端的完整过程。
摘要由CSDN通过智能技术生成

netty(二)–使用Marshalling编解码java对象

一、简介

marshalling是jboss的java对象序列化包,修正了jdk原生序列化存在的问题,保持了对java.io.Serializable接口的兼容,这里介绍以marshalling为编解码工具在netty中的使用。

二、代码示例

2.1 添加maven依赖
<dependency>
  <groupId>io.netty</groupId>
  <artifactId>netty-all</artifactId>
  <version>5.0.0.Alpha2</version>
</dependency>
<dependency>
  <groupId>org.jboss.marshalling</groupId>
  <artifactId>jboss-marshalling-serial</artifactId>
  <version>2.0.6.Final</version>
</dependency>
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.6</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.46</version>
</dependency>
2.2 定义消息对象
package com.dragon.study.netty;

import lombok.Data;
import java.io.Serializable;

@Data
public class Msg implements Serializable {
   
    private String id;
    private String content;

    public Msg(String id, String content) {
   
        this.id = id;
        this.content = content;
    }
}
2.3 定义marshalling编解码工具
package com.dragon.study.netty;

import io.netty.handler.codec.marshalling.*;
import org.jboss.marshalling.MarshallerFactory;
import org.jboss.marshalling.Marshalling;
import org.jboss.marshalling.MarshallingConfiguration;

public class MarshallingFactory {
   

    //生成Marshalling工厂类
    private static MarshallerFactory mf = Marshalling.getProvidedMarshallerFactory("serial");
    private static MarshallingConfiguration conf = new MarshallingConfiguration
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值