Search code examples
kotlinspring-rabbit

kotlin object (local final class) not serializable


For Some reason, I need to construct an object in kotlin like below and need to send to rabbitMQ using rabbitTemplate.convertAndSend. But when I do that, I get an error

       var myObject = object {
            var name = "Object Name"
            var description = "Object Description"
        }

Error

SimpleMessageConverter only supports String, byte[] and Serializable payloads

What should I do to make myObject as Serializable?


Solution

  • Your object is not Serializable. To fix try:

    var myObject = object : Serializable {
        var name = "Object Name"
        var description = "Object Description"
    }
    

    Keep in mind, that for consistent deserialization in case of class changes you need to declare constant serialVersionUID at companion object, but they are not available to local class.