我正在使用带有org.springframework.boot:spring-boot-starter-data-rest
的kotlin 1.1.51和2.0.0.M6的spring boot,并尝试格式化json的输出。
我该如何配置。弹簧休息数据,以便它将输出/接受外键,而不是在ManyToOne-Relationship中具有href的对象?
带有外键language_id
的示例模型(kotlin):
@Entity
data class Song (
@Column
var title: String,
@Column
var songTypeId: Int,
@OneToMany(mappedBy = "song")
var mastersheets: Set<Mastersheet> = setOf<Mastersheet>()
) : UpdateableBaseEntity() {
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "language_id", nullable = false)
lateinit var language: Language
@ManyToOne()
@JoinColumn(name = "artist_id", nullable = false)
lateinit var artist: Artist
}
输出类似于:
{
"_embedded" : {
"songs" : [ {
"title" : "Hello World",
"songTypeId" : 1,
"insertDate" : "2018-09-06T07:25:14.307+0000",
"updateDate" : null,
"_links" : {
"self" : {
"href" : "http://localhost/songs/1"
},
"song" : {
"href" : "http://localhost/songs/1"
},
"artist" : {
"href" : "http://localhost/songs/1/artist"
},
"mastersheets" : {
"href" : "http://localhost/songs/1/mastersheets"
},
"language" : {
"href" : "http://localhost/songs/1/language"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost/songs"
},
"profile" : {
"href" : "http://localhost/profile/songs"
}
}
}
注意:我也尝试了这种配置
@Bean
fun repositoryRestConfigurer(): RepositoryRestConfigurer {
return object : RepositoryRestConfigurerAdapter() {
override fun configureRepositoryRestConfiguration(config: RepositoryRestConfiguration?) {
config!!.exposeIdsFor(Language::class.java)
}
}
}
答案 0 :(得分:0)
我在这里https://stackoverflow.com/a/38165158/2248405找到了解决方案
使用SpringExpression语言(SpEL),可以将所有内容都放在同一个对象中:
@Projection(name = "fullDetails", types = arrayOf(Song::class))
interface SongsFullDetailsProjection {
fun getId(): Long
fun getTitle(): String
@Value("#{target.getArtist()?.getId()}")
fun getArtistId(): Long?
@Value("#{target.getSongType()?.getId()}")
fun getSongTypeId(): Long?
@Value("#{target.getLanguage()?.getId()}")
fun getLanguageId(): Long?
}