java - @JsonView 注释不起作用

标签 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')
}

如果需要,我可以提供更多代码。提前致谢。

附注当从方法中删除 @JsonView 注释时,相同的代码可以完美运行

此外,我正在使用 IntelliJ Idea

最佳答案

  1. 正如 FasterXML 的文档中所述。如果使用 proguard,kotlin.Metadata 注释可能会被删除,从而阻止反序列化。添加 proguard 规则以保留 kotlin.Metadata 类: -keep class kotlin.Metadata { *; }

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

关于java - @JsonView 注释不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51165159/

相关文章:

java - Checkmarx SQL 注入(inject)高严重性问题

java - Hibernate validator 导致多对多关系无法保存

java - 任务 ':compileTestJava'的Gradle执行失败

intellij-idea - 如何更改 IntelliJ 选项卡符号?

java - Log4j2 用零填充文件名

java - 外键作为主键注释

java - 从 Ninite.com 更新后 Eclipse 无法打开

java - 使用@Formula hibernate ManyToOne

mysql - 在 Hibernate 中使用密码(对于独立应用程序)

java - 如何让 IntelliJ 颜色预览识别自定义颜色类?