kotlin中的数字是不可序列化的
我发现kotlin中的数字是不可序列化的。
- 第一个问题
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<Device, Long?> { public fun findByName(Param("name") name: String): List<Device> }
当我尝试编译此代码时,出现错误,因为kotlin.Long不是可序列化的 :
错误:(14,72)Kotlin:类型参数不在其范围内:应该是“java.io.Serializable?”的子类型
- 第二个问题
我尝试使用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<Device, java.lang.Long?> { public fun findByName(Param("name") name: String): List<Device> }
警告:(14,72)Kotlin:Kotlin不应该使用这个类。 改用kotlin.Long。
错误:(14,72)Kotlin:类型参数不在其范围内:应该是“java.io.Serializable?”的子类型
Kotlin 1.0 Beta 1原始类型是可序列化的:
Int是可串行化的
现在,类型Int和其他基本类型在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<Device, DeviceId?> { public fun findByName(Param("name") name: String): List<Device> }
这个用例工作正常
我偶然发现了同样的问题,我设法通过在java中存储我的Repository接口来处理它,在那里我把java.lang.Long作为id的泛型类型参数。 其余的依然是kotlin(数据类,服务类等)
- Spring数据Elasticsearch与Kotlin数据类的脚本字段导致Jackson序列化问题
- Kotlin + Spring Boot请求编组
- Spring Security,Method Security注解(@Secured)不工作(java config)
- 相同的其他端点具有不同的PathVariable
- Spring框架吞下自定义转换器的异常
- 与AsyncRestTemplate Netty客户端的Spring启动失败
- Spring Data JPA:处理其他实体的继承属性
- 如何在基于Spring的强类型语言中正确地使用PATCH – example
- Spring WebFlux:Reactive MongoDB