gpt4 book ai didi

java - @JsonView 注释不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 10:21:08 25 4
gpt4 key购买 nike

我正在尝试使用 @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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com