简体   繁体   中英

Java Record Data Type Doesn't Work For Network Communication

I thought the new Java Record data type in Java 14 should be suitable for data crossing the network because the data shouldn't change in the process which is the main feature of the record data type - immutable.

I use the data type in RSocket. On the sender side, I get an error on encoder. I haven't found what the encoder is about. On the receiver side, I get a deserializer error. To my knowledge, a record can't implement an interface such as the Serializable.

So, the record data type doesn't work for network programming, is that right?

Update:

I add the Serializable interface to the record definition on the both sender and receiver sides. After sending out a request, it is fine on the TCP sender side, but an error on the receiver side

No encoder for com.....data.Notification, current value type is class com.....data.Notification

Have those who marked this question have a different experience?

To my knowledge, a record can't implement an interface such as the Serializable.

It can. Just add implements Serializable to its declaration. It cannot extend any other class because it already extends Record , if that's what you mean.

See JEP-359

Beyond the restrictions above, records behave like normal classes: they can be declared top level or nested, they can be generic, they can implement interfaces , and they are instantiated via the new keyword.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM