Jackson JsonIgnore 在 kotlin 中不起作用

VSM*_*ent 2 java spring json jackson kotlin

我正在将 Java Spring 应用程序重写为 Kotlin Spring 应用程序。

除了对 openweather 的 API 请求外,一切正常。

要将 DTO 存储在数据库中,有一个id字段和cityId从 API 检索的字段(在那里调用id)。

出于某种原因,@JsonIgnore 不适用于 DTO id 字段。

构建.gradle

// plugins

    id 'org.springframework.boot' version '2.2.4.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
    id 'war'
    id 'maven'
    id 'org.jetbrains.kotlin.jvm' version '1.3.70'
    id "org.jetbrains.kotlin.plugin.jpa" version "1.3.70"
    id "org.jetbrains.kotlin.plugin.noarg" version "1.3.70"
    id "org.jetbrains.kotlin.plugin.spring" version "1.3.70"
    id "org.jetbrains.kotlin.plugin.allopen" version "1.3.70"


// dependencies

    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-mail:2.2.4.RELEASE'
    implementation 'org.springframework.security:spring-security-test'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    implementation "org.jetbrains.kotlin:kotlin-reflect"
    implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.10.2"
Run Code Online (Sandbox Code Playgroud)

OpenWeather 响应下一个 JSON(省略了一些字段):

{
    "coord":{
        "lon":-0.13,
        "lat":51.51
    },
    "main":{
        "temp":14.04,
        "feels_like":7.05,
        "pressure":1011,
        "humidity":61
    },
    "dt":1584018901,
    "id":2643743,              <- cityId in DTO class
    "name":"London",
 ...
}
Run Code Online (Sandbox Code Playgroud)

DTO类:


import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty

@JsonIgnoreProperties(ignoreUnknown = true)
data class OpenWeatherApiDto(
        @JsonIgnore
        override var id: Long? = null,
        ...
        override var cityId: Int? = null,
        ...
) : AbstractWeatherSampleDto() {
    ...
    @JsonProperty("id")
    private fun unpackId(idObj: Int?) {
        cityId = idObj
                ?: throw ApiFieldNotFoundException("id")
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

失败的测试

@Test
    fun createFromFile() {
        val mapper = jacksonObjectMapper()
        Files.lines(Paths.get("src/test/resources/data/OWApi.json")).use { s ->
            val json: String = s.collect(Collectors.joining())
            val ws: OpenWeatherApiDto = mapper.readValue(json)
            println(ws)
            assertThat(ws)
                    .isNotNull
                    .extracting("cityId").isEqualTo(2643743)
        }
    }
Run Code Online (Sandbox Code Playgroud)

失败消息是:

[Extracted: cityId] 
Expecting:
 <null>
to be equal to:
 <2643743>
but was not.
Run Code Online (Sandbox Code Playgroud)

实际对象是:

OpenWeatherApiDto( id=2643743 , cityName=London, temperature=14.04, FeelLike=7.05, pressure=1011.0, 湿度=61, cloud=null , cityId=null , time=1584018901, latitude=51.51, longitude=-0.

我在jackson-module-kotlin GitHub 页面上发现了类似的问题,当我使用2.10.2 时,它恰好在2.9.6 中jackson-databind 相关解决

gid*_*dds 11

我还没有尝试过使用您正在使用的确切版本,但我怀疑如果您将其替换@JsonIgnore@get:JsonIgnore.

这是将 Java 转换为 Kotlin 时常见的问题之一。在 Java 中,您通常使用私有字段和 getter 方法(如果它是可变的,还有一个 setter)来实现一个属性。您还可以包含一个构造函数参数来初始化它。这最多是四个独立的代码位,都与同一个属性相关——每个都可以有自己的注释。

但是,在 Kotlin 中,您可以从一段代码中获得所有这些。(任何非私有属性都会为您提供一个私有字段和一个 getter 方法;如果是这样,var您还会获得一个 setter;如果它在主构造函数中,那么您还会获得一个构造函数参数。)这要简洁得多!

但是任何注释都适用于什么?

默认情况下,它们应用于构造函数参数——或者,如果属性在类体中定义,则属性本身(仅对 Kotlin 可见)。因此,如果您希望它们应用于该字段,或者应用于 getter 或 setter,则需要明确指定,例如使用@field:JsonIgnore@get:JsonIgnore

完整的细节在Kotlin 文档中

  • 感谢您的回答,但不幸的是它不起作用。与 .@get:JsonProperty / .@set:JsonProperty / .@field:JsonProperty / .@get:JsonProperty .@set:JsonProperty .@JsonProperty 中的任何一个一起使用。Id 转到 id( (2认同)
  • 我在 val 上使用了 get() ,它按我的预期工作。 (2认同)

VSM*_*ent 1

我没有找到解决方案,但与

    @JsonProperty("_id")
    override var id: Long? = null,
Run Code Online (Sandbox Code Playgroud)

它按预期工作。