在Json Response中访问_embedded

时间:2019-03-28 21:27:55

标签: json angular typescript spring-data-jpa

无法访问Angular的Json中的_embedded属性。可能吗 使用Spring-Data-Jpa创建的Json响应如下所示:

{
"_embedded": {
    "reviews": [
        {
            "author": "Anonymous",
            "content": "This movie sucks",
            "upvotes": 0,
            "downvotes": 0,
            "_links": {
                "self": {
                    "href": "http://localhost:8080/api/reviews/1"
                },
                "review": {
                    "href": "http://localhost:8080/api/reviews/1"
                },
                "film": {
                    "href": "http://localhost:8080/api/reviews/1/film"
                }
            }
        }, ...
    ]
},
"_links": {
    "self": {
        "href": "http://localhost:8080/api/reviews{?page,size,sort}",
        "templated": true
    },
    "profile": {
        "href": "http://localhost:8080/api/profile/reviews"
    }
},
"page": {
    "size": 20,
    "totalElements": 6,
    "totalPages": 1,
    "number": 0
}

} 我想从我的客户应用程序处理嵌套在嵌入式字段中的评论。但是我无法进入_embedded属性。我的Angular服务如下所示:

getEmbedded(): Observable<any> {
return this.http.get(ServerConfig.serverAddress + '/api/reviews');
}

使用服务的组件:

export class ReviewListComponent implements OnInit {
embeddedReviewResource: any;

constructor(private reviewService: ReviewService) { }

ngOnInit() {

this.reviewService.getEmbedded().subscribe(data => {
  this.embeddedReviewResource = data;
});
}

我认为以多种方式引用_embedded属性:

*ngFor="let review of embeddedReviewResource.reviews

*ngFor="let review of embeddedReviewResource._embedded.reviews

*ngFor="let review of embeddedReviewResource.getEmbedded().reviews

*ngFor="let review of embeddedReviewResource._embeddedViews.reviews

所有这些似乎都无法解决问题。如何访问Json文件中的_embedded属性? 完整的源代码可在my GitHub上获得。

1 个答案:

答案 0 :(得分:1)

   reviews = []; 

   this.reviewService.getEmbedded().subscribe(data => {
      this.reviews = data._embedded.reviews;
    });

模板

   <div *ngFor="let review of reviews"> 
     {{ review.content }}
   </div>