Spring Data Rest一对多JSON格式:使用外键代替对象 [英] Spring Data Rest one to many JSON Formatting: Use foreign key instead of objects

查看:169
本文介绍了Spring Data Rest一对多JSON格式:使用外键代替对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Spring Boot与2.0.0.M6,kotlin 1.1.51和org.springframework.boot:spring-boot-starter-data-rest一起使用并尝试格式化json的输出.

Im using spring boot with 2.0.0.M6 with kotlin 1.1.51 with org.springframework.boot:spring-boot-starter-data-rest and trying to format output of json.

我该如何配置.弹簧休息数据,以便它将输出/接受外键,而不是在ManyToOne-Relationship中具有href的对象?

How can i config. spring rest data so that it will output/accept foreign key instead of object with href in a ManyToOne-Relationship?

带有外键language_id(kotlin)的示例模型:

sample model with foreign key 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"
    }
  }
}

注意:我也尝试了这种配置

Note: I also tried this configuration

@Bean
fun repositoryRestConfigurer(): RepositoryRestConfigurer {
    return object : RepositoryRestConfigurerAdapter() {
        override fun configureRepositoryRestConfiguration(config: RepositoryRestConfiguration?) {
            config!!.exposeIdsFor(Language::class.java)
        }
    }
}

推荐答案

我在这里找到了我的解决方案 https://stackoverflow.com/a/38165158/2248405

I found my solution here https://stackoverflow.com/a/38165158/2248405

使用SpringExpression语言(SpEL),可以将所有内容都放在同一个对象中:

With SpringExpression Language (SpEL) it is possible to have everything in the same object:

@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?

}

这篇关于Spring Data Rest一对多JSON格式:使用外键代替对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆