spring的数据mongodb和kotlin
使用弹簧数据mongodb使用kotlin时,我遇到了一个问题。 当我尝试从mongodb读取对象时,我得到一个错误,抱怨说我的数据类没有默认的无参数构造函数。 我可以通过给我的数据类中的每个字段的值来解决这个问题,所以编译器会生成一个默认的无参数构造函数。 当然,我不是真的想这样做。
我知道有一个jacksonkotlin模块,它包含在我的maven文件中。 它适用于反序列化对象,我得到了HTTP,所以我知道spring拿起它。 但似乎spring的数据mongodb不使用jackson对象映射器?
有没有办法我可以在弹簧数据mongodb中使用jackson对象映射器或修复没有非参数构造函数的问题?
现在有一个无Arg插件 。 你可以在你的Gradle或Maven Build Script中添加这个插件,并运行你的代码而无需Boilerplate或者你的variables的默认值。
Gradle简单的例子:
添加插件build.grade
buildscript { ext { kotlinVersion = '1.1.2' } repositories { mavenCentral() } dependencies { classpath("org.jetbrains.kotlin:kotlin-noarg:${kotlinVersion}") } } apply plugin: "kotlin-noarg" // apply "kotlin-jpa" for JPA related annotation noArg { // define annotation, where you need an empty custructor annotation("org.springframework.data.mongodb.core.mapping.Document") }
创建您的文档
@Document(collection = "simpleDoc") class SimpleDocument(@Id val id: String, @Field val name: String)
使用Spring-Data-REST创建一个插入
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/hal+json' -d '{"name":"awesome"}' 'http://localhost:8080/docs'
阅读你的文档
curl -X GET --header 'Accept: application/hal+json' 'http://localhost:8080/docs/59284d508bc7ee0b4c8fe293'
- Spring Boot将文本/ JavaScript序列化为JSON
- 这个groovy语法如何转换成kotlin?
- 我可以写sampleVideoView吗? =在Kotlin中的findViewById <VideoView>(R.id.videoView)?
- 如何管理Kotlin中的单元测试资源,如启动/停止数据库连接或嵌入式elasticsearch服务器?
- 通过JSON配置选择策略
- Kotlin协程中的“+”?
- 将Kotlin添加到现有Java项目会打破Android Studio gradle消息错误
- 错误:类型推断失败
- 如何从Firebase数据库检索数据并将其存储在ListArray <Class>中? – Kotlin