@JsonView注释不起作用

时间:2018-07-04 02:56:25

标签: java spring intellij-idea kotlin jackson

我正在尝试使用@JsonView批注从Spring反序列化对象中的多个字段。但是,我将注释添加到方法中,而不是反序列化指定字段,而是返回一个空对象。

这是我的POJO:

@Entity
data class Album(
        @JsonView(View.AlbumSummary::class)
        val title: String,

        @JsonView(View.AlbumSummary::class)
        @ManyToMany
        val artists: List<Account>,

        val dateReleased: LocalDate,

        val genre: String = GENRE_NA,

        @OneToMany(mappedBy = "album")
        val songs: List<Song> = ArrayList(),

        val description: String = ""
)

此外,实现@JsonView批注的方法:

@JsonView(View.AlbumSummary::class)
@RequestMapping("/home-recommendations/{userId}")
fun getHomeRecommendations(@PathVariable userId: String): List<Recommendation> {

    val recommendations = ArrayList<Recommendation>()

    val user = accountRepository.findById(userId).get()
    val followingArtists = user.following.filter {
        it.following.isArtist
    }
    val suggestedArtists = followingArtists.shuffled().take(Random().nextInt(11) + 10)

    for (i in 0 until suggestedArtists.size) {

        val suggestedArtist = suggestedArtists[i].following

        val recommendedAlbums = suggestedArtist.albums.shuffled().take(Random().nextInt(6) + 10)

        recommendations.add(Recommendation("Because you listened to ${suggestedArtist.fullName}", Recommendation.TYPE_ALBUM, albums = recommendedAlbums))

    }

    return recommendations

}

编辑:我的gradle配置:

buildscript {
    ext {
        kotlinVersion = '1.2.50'
        springBootVersion = '2.0.3.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-noarg:${kotlinVersion}")
    }
}

apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'kotlin-jpa'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.aceinteract'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile('org.springframework.boot:spring-boot-starter-mustache')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('com.fasterxml.jackson.module:jackson-module-kotlin')
    compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    compile("org.jetbrains.kotlin:kotlin-reflect")
    runtime('org.postgresql:postgresql')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')
}

如果需要,我可以提供更多代码。预先感谢。

P.S。将@JsonView注释从方法中删除后,相同的代码可以完美工作

此外,我正在使用IntelliJ Idea

3 个答案:

答案 0 :(得分:0)

确保您的类路径上有Jackson Module for Kotlin并在ObjectMapper实例上注册。

答案 1 :(得分:0)

  1. FasterXML文档中所述。如果使用 proguard ,则可能会删除kotlin.Metadata批注,以防止反序列化。添加一个proguard规则来保留kotlin.Metadata类:-keep class kotlin.Metadata { *; }

  2. 尝试使用@field:JsonView(View.AlbumSummary::class)

答案 2 :(得分:0)

我遇到了同样的问题,只有在将字段移出构造函数并移到类主体中之后,JsonView才起作用。在这种情况下,您不会使用数据类,但仍可以将equals哈希码toString委派给私有内部数据类。