默认情况下返回toString()\u id mongo spring boot kotlin

默认情况下返回toString()\u id mongo spring boot kotlin,spring,mongodb,spring-boot,kotlin,Spring,Mongodb,Spring Boot,Kotlin,嗨,我是Kottling的新手,我正在使用mongoDB作为数据库进入REST服务 我试图从数据库及其作品中获取信息,但_id显示为对象而不是字符串 回应 [ { "id": { "timestamp": 1622599648, "date": "2021-06-02T02:07:28.000+00:00" },

嗨,我是Kottling的新手,我正在使用mongoDB作为数据库进入REST服务 我试图从数据库及其作品中获取信息,但_id显示为对象而不是字符串

回应

[
    {
        "id": {
            "timestamp": 1622599648,
            "date": "2021-06-02T02:07:28.000+00:00"
        },
        "name": "test",
        "description": "test",
        "createdDate": "2021-06-01T21:07:28.385",
        "modifiedDate": "2021-06-01T21:07:28.385"
    },
    {
        "id": {
            "timestamp": 1622600161,
            "date": "2021-06-02T02:16:01.000+00:00"
        },
        "name": "test",
        "description": "test",
        "createdDate": "2021-06-01T21:16:01.669",
        "modifiedDate": "2021-06-01T21:16:01.669"
    }
]
类别:

@Document
data class Patient (
        @Id
        
        val id: ObjectId = ObjectId.get(),
        val name: String,
        val description: String,
        val createdDate: LocalDateTime = LocalDateTime.now(),
        val modifiedDate: LocalDateTime = LocalDateTime.now()
        )

interface PatientRepository : MongoRepository<Patient, String> {
        fun findOneById(id: ObjectId): Patient
        override fun deleteAll()
}
@文档
数据类患者(
@身份证
val id:ObjectId=ObjectId.get(),
val name:String,
val说明:字符串,
val createdDate:LocalDateTime=LocalDateTime.now(),
val modifiedDate:LocalDateTime=LocalDateTime.now()
)
接口PatientPository:MongorPository{
fun findOneById(id:ObjectId):患者
覆盖所有
}

如何将id转换为字符串?当我检索数据时?

您需要使用序列化程序将ObjectId转换为带有bean的字符串

@Bean
    fun customizer(): Jackson2ObjectMapperBuilderCustomizer? {
        return Jackson2ObjectMapperBuilderCustomizer { builder: Jackson2ObjectMapperBuilder -> builder.serializerByType(ObjectId::class.java, ToStringSerializer()) }
    }
政府的回应是:

[
    {
        "id": "60b6e7e03c52e16a1a1b000c",
        "name": "test",
        "description": "test",
        "createdDate": "2021-06-01T21:07:28.385",
        "modifiedDate": "2021-06-01T21:07:28.385"
    },
    {
        "id": "60b6e9e11194486befd09e8c",
        "name": "test",
        "description": "test",
        "createdDate": "2021-06-01T21:16:01.669",
        "modifiedDate": "2021-06-01T21:16:01.669"
    }
]

我不知道科特林是怎么工作的。您提到了spring boot,有时您可能会从@varman那里得到一些想法,谢谢!