Java Serializable Object to Byte Array

后端 未结 12 1480
春和景丽
春和景丽 2020-11-22 03:12

Let\'s say I have a serializable class AppMessage.

I would like to transmit it as byte[] over sockets to another machine where it is rebuil

12条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 03:22

    If you are using spring, there's a util class available in spring-core. You can simply do

    import org.springframework.util.SerializationUtils;
    
    byte[] bytes = SerializationUtils.serialize(anyObject);
    Object object = SerializationUtils.deserialize(bytes);
    

提交回复
热议问题