• JAVA中常用序列化与反序列化合集


    一:RabbitMQ发送消息和接收消息的数据是需要进行序列化的,具体的序列化方式如下:

    1.序列化对象
    实体类需实现Serializable接口;
    生产者和消费者中的包名、类名、属性名必须一致;
    生产者和消费者使用的queue队列一致
    
    1.1生产者
    @Service
    public class MQService {
    
    @Resource
    private AmqpTemplate amqpTemplate;
    
    public void sendGoodsToMq(Dog dog){
    //消息队列可以发送 字符串、字节数组、序列化对象
    amqpTemplate.convertAndSend("","queue_A",dog);
        }
    }
    
    1.2消费者
    @Component
    public class ConsumerService {
    
    @RabbitListener(queues = "queue_A")
    @RabbitHandler
    public void consumeMessage(Dog dog){
    System.out.println("dog---"+dog);
        }
    }
    
    2.序列化字节数组
    实体类需实现Serializable接口;
    生产者和消费者中的包名、类名、属性名必须一致;
    生产者和消费者使用的queue队列一致
    
    2.1 生产者
    @Service
    public class MQService {
    
    @Resource
    private AmqpTemplate amqpTemplate;
    
    public void sendGoodsToMq(Dog dog){
    //消息队列可以发送 字符串、字节数组、序列化对象
    byte[] bytes = SerializationUtils.serialize(dog);
    amqpTemplate.convertAndSend("","queue_A",bytes);
        }
    }
    
    2.2消费者
    @Component
    public class ConsumerService {
    
    @RabbitListener(queues = "queue_A")
    @RabbitHandler
    public void consumeMessage(byte[] bs){
    Dog dog = (Goods) SerializationUtils.deserialize(bs);
    System.out.println("byte[]---"+dog);
        }
    }
    
    3.JSON字符串
    对象的属性名需一致
    
    3.1 生产者
    @Service
    public class MQService {
    
    @Resource
    private AmqpTemplate amqpTemplate;
    
    public void sendGoodsToMq(Dog dog) throws JsonProcessingException {
    //消息队列可以发送 字符串、字节数组、序列化对象
    ObjectMapper objectMapper = new ObjectMapper();
    String msg = objectMapper.writeValueAsString(dog);
    amqpTemplate.convertAndSend("","queue_A",msg);
        }
    
    }
    
    3.2消费者
    登录后复制
    @Component
    public class ReceiveService {
    
    @RabbitListener(queues = "queue_A")
    @RabbitHandler
    public void receiveMsg(String msg) throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    Dog dog = objectMapper.readValue(msg,Dog.class);
    System.out.println("String---"+msg);
        }
    }
    -----------------------------------
    Java中使用RabbitMQ传递对象
    参考文章:
    https://blog.51cto.com/u_15060547/4382954
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96

    二:LocalDateTime的序列化

    1:引入Maven文件

    <dependency>
                <groupId>com.fasterxml.jackson.datatype</groupId>
                <artifactId>jackson-datatype-jsr310</artifactId>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4

    需要在LocalDateTime实例属性中添加下面的注解

    后续更新中…

  • 相关阅读:
    HTML5七夕情人节表白网页制作【空中散落的花瓣3D相册】HTML+CSS+JavaScript
    Codeforces Round 884 (Div. 1 + Div. 2)C. Particles(找性质、奇偶性)
    (仿牛客社区项目)Java开发笔记7.7:生成长图
    R语言ggplot2可视化:可视化条形图(bar plot)、添加主标题、副标题、题注信息(caption)、自定义轴标签文本的角度
    C++入门教程(八、枚举类型)
    集成学习 #数据挖掘 #Python
    法律法规分论坛 | 筑牢数据合规“堤坝”
    Flink watermark与乱序消息处理机制
    ai批量剪辑矩阵无人直播一站式托管系统源头技术开发
    进程的状态
  • 原文地址:https://blog.csdn.net/weixin_43328098/article/details/125537802