2017-06-03 55 views
1

我有兩個實體定義:暴露在春子對象的領域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; 

    //... 

} 

我有以下JSON響應政府號召到/ API/videoposts:

"_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" 
} 

我怎麼能做到這一點?

+0

是否使用彈簧數據休息提供隱藏這些嵌套的對象,你的對象,你想要的方式HTTP端點還是您編寫了自己的控制器並使用'org.springframework.hateoas.Resource'來呈現資源? – systemfreund

回答

0

您可以檢查Projections在這裏你可以自定義你是否要顯示整個嵌套的屬性或他們中的一些甚至