首页 > 解决方案 > 如何在 json 返回对象 spring boot 中包含 Embedded 属性

问题描述

我有一个带有以下实体的 Kotlin Spring Boot 应用程序:

import com.fasterxml.jackson.annotation.JsonIgnore
import javax.persistence.*

@Entity
@Table(name = "users")
class Users(
    @Id
    var username: String = "",

    @JsonIgnore
    var password: String = "",

    @JsonIgnore
    var enabled: Boolean = false,

    @ElementCollection
    @CollectionTable(name = "authorities", joinColumns = [JoinColumn(name = "username")])
    @MapKeyColumn(name = "users")
    private var authorities : MutableSet<Authorities> = mutableSetOf()
)

@Embeddable
class Authorities(
    var authority: String
)

当我调用此端点时:

@GetMapping("/users")
fun getUserById(@RequestParam(value = "username") username : String) : ResponseEntity<Users>{
    val optionalUser = usersService.getByUsername(username)
    return if(optionalUser.isPresent) ResponseEntity.ok(optionalUser.get())
    else ResponseEntity(HttpStatus.BAD_REQUEST)
}

它正确返回,如您在此 json 中所见:

{
    "username": "test@gmail.com"
}

但它不会返回属于该用户的权限列表,我想让它返回。我的问题是:我该怎么做?

非常感谢你的帮助

标签: spring-bootkotlinjpajackson

解决方案


推荐阅读