在春天hateoas暴露儿童对象的领域

时间:2017-06-03 12:00:27

标签: spring spring-rest spring-hateoas

我定义了两个实体:

@Entity
public class VideoPost {
    private @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY) Long id;
    private String videoTitle;
    private @ManyToOne @JoinColumn(name = "VideoPost_Id") User uploader;
    private boolean isPublished = false;

    //....
}


@Entity
public class User {
    private @Id @GeneratedValue(strategy=GenerationType.IDENTITY) Long id;
    private String userName;
    private Date registrationDate;

    @OneToMany(mappedBy = "uploader", cascade = CascadeType.ALL)  private List<VideoPost> videoPosts;

    //...

}

我对/ api / videoposts的调用有以下JSON响应:

"_embedded" : {
    "videoPosts" : [ {
      "videoTitle" : "test video 1",
      "uploadDate" : "2017-06-03T11:44:02.012+0000",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/api/videoPosts/1"
        },
        "videoPost" : {
          "href" : "http://localhost:8080/api/videoPosts/1"
        },
        "uploader" : {
          "href" : "http://localhost:8080/api/videoPosts/1/uploader"
        }
      }
    }   ]

我想直接在此响应中公开上传者名称。即。

"uploader" : {
    "userName": theName
    "href" : "http://localhost:8080/api/videoPosts/1/uploader"
}

我怎么能实现这个目标?

1 个答案:

答案 0 :(得分:0)

您可以检查Projections,您可以按照自己的方式自定义对象,无论您是要显示整个嵌套属性还是其中一些属性,甚至隐藏这些嵌套对象