gpt4 book ai didi

spring - kotlin 中的数字不可序列化

转载 作者:行者123 更新时间:2023-12-04 20:05:16 25 4
gpt4 key购买 nike

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

  • 第一个问题

  • 设备.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 不是 可序列化 :

    Error:(14, 72) Kotlin: Type argument is not within its bounds: should be subtype of '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>
    }

    Warning:(14, 72) Kotlin: This class shouldn't be used in Kotlin. Use kotlin.Long instead.

    Error:(14, 72) Kotlin: Type argument is not within its bounds: should be subtype of 'java.io.Serializable?'

    最佳答案

    从 Kotlin 1.0 Beta 1 开始,原始类型是可序列化的:

    Int is Serializable

    Now the type Int and other basic types are Serializable on the JVM. This should help many frameworks.


    从:
    http://blog.jetbrains.com/kotlin/2015/10/kotlin-1-0-beta-candidate-is-out/
    因此,您不再有任何问题。

    关于spring - kotlin 中的数字不可序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26196796/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com