kotlin中的数字是不可序列化的

我发现kotlin中的数字是不可序列化的。

  1. 第一个问题

Device.kt:

package test.domain import javax.persistence.* Entity public class Device { public Id GeneratedValue var id: Long = -1 public var name: String = "" ... } 

DeviceRestRepository.kt:

 package test.domain import org.springframework.data.repository.PagingAndSortingRepository import org.springframework.data.repository.query.Param import org.springframework.data.rest.core.annotation.RepositoryRestResource RepositoryRestResource(collectionResourceRel = "device", path = "device") public trait DeviceRestRepository : PagingAndSortingRepository { public fun findByName(Param("name") name: String): List } 

当我尝试编译这段代码时,出现错误,因为kotlin.Long不是可序列化的

错误:(14,72)Kotlin:types参数不在其范围内:应该是“java.io.Serializable?”的子types

  1. 第二个问题

我尝试使用java.lang.Long时遇到同样的错误:

DeviceRestRepository.kt:

 package test.domain import org.springframework.data.repository.PagingAndSortingRepository import org.springframework.data.repository.query.Param import org.springframework.data.rest.core.annotation.RepositoryRestResource RepositoryRestResource(collectionResourceRel = "device", path = "device") public trait DeviceRestRepository : PagingAndSortingRepository { public fun findByName(Param("name") name: String): List } 

警告:(14,72)Kotlin:Kotlin不应该使用这个类。 改用kotlin.Long。

错误:(14,72)Kotlin:types参数不在其范围内:应该是“java.io.Serializable?”的子types

Kotlin 1.0 Beta 1原始types是可序列化的:

Int是可串行化的

现在,typesInt和其他基本types在JVM上是Serializable。 这应该有助于许多框架。

来自: http : //blog.jetbrains.com/kotlin/2015/10/kotlin-1-0-beta-candidate-is-out/

因此你不再有任何问题。

我发现这个问题的解决方法:

Device.kt:

 package test.domain import javax.persistence.* Entity public class Device { public EmbeddedId var id: DeviceId = DeviceId() public var name: String = "" ... } Embeddable public class DeviceId: Serializable { public GeneratedValue var id: Long = -1 } 

DeviceRestRepository.kt:

 package test.domain import org.springframework.data.repository.PagingAndSortingRepository import org.springframework.data.repository.query.Param import org.springframework.data.rest.core.annotation.RepositoryRestResource RepositoryRestResource(collectionResourceRel = "device", path = "device") public trait DeviceRestRepository : PagingAndSortingRepository { public fun findByName(Param("name") name: String): List } 

这个用例工作正常

我偶然发现了同样的问题,我设法通过在java中存储我的Repository接口来处理它,在那里我把java.lang.Long作为id的genericstypes参数。 其余的依然是kotlin(数据类,服务类等)