私はすべての記事を返すリクエストハンドラメソッド "listAll"を持っています。このメソッドを呼び出すと記事リストが返されますが、anglejs側でこのリターンにアクセスすると、ユーザー情報が含まれません。たとえば、article.user.nameは情報を返しませんが、他の記事フィールドにアクセスできます。私のエンティティ、anglejsのスクリプトとコントローラは以下の通りです。Spring RestとAngularjsでエンティティのオブジェクト情報を取得する方法は?
@RequestMapping(method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<?> listAll(@RequestParam int page, Locale locale) {
return createListAllResponse(page, locale);
}
Angularjsスクリプト:
それは返す "data.articles [i]を.user" 次のスクリプトで定義されていない:
おかげで、あなたのために@Entity
public class Article {
@Id
@GeneratedValue
private int id;
private String title;
private String summary;
private String description;
private String createdDate;
@ManyToOne
private User user;
public Article(){
}
public Article(String title, String summary, String description, String createdDate, int id, User user) {
super();
this.title= title;
this.summary= summary;
this.description = description;
this.createdDate = createdDate;
this.user = user;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCreatedDate() {
return createdDate;
}
public void setCreatedDate(String createdDate) {
this.createdDate = createdDate;
}
@JsonBackReference
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public boolean equals(Object object) {
if (object instanceof Article){
Article article = (Article) object;
return article.id == id;
}
return false;
}
@Override
public int hashCode() {
return id;
}
}
@Entity
@Table(name = "system_user")
public class User {
@Id
@GeneratedValue
private int id;
private String email;
private String name;
private String enabled;
private String password;
@Enumerated(EnumType.STRING)
@Column(name = "user_role")
private Role role;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEnabled() {
return enabled;
}
public void setEnabled(String enabled) {
this.enabled = enabled;
}
}
リクエストハンドラメソッドを支援します
$scope.populateTable = function (data) {
if (data.pagesCount > 0) {
$scope.state = 'list';
$scope.page = {source: data.articles, currentPage: $scope.pageToGet, pagesCount: data.pagesCount, totalArticles : data.totalArticles};
alert("data.articles.length: " + data.articles.length)
for (var i = 0; i < data.articles.length; i++) {
alert("data.articles ["+i+"]: "+ data.articles[i].user.name);
}
if($scope.page.pagesCount <= $scope.page.currentPage){
$scope.pageToGet = $scope.page.pagesCount - 1;
$scope.page.currentPage = $scope.page.pagesCount - 1;
}
$scope.displayCreateArticleButton = true;
$scope.displaySearchButton = true;
} else {
$scope.state = 'noresult';
$scope.displayCreateArticleButton = true;
if(!$scope.searchFor){
$scope.displaySearchButton = false;
}
}
if (data.actionMessage || data.searchMessage) {
$scope.displayMessageToUser = $scope.lastAction != 'search';
$scope.page.actionMessage = data.actionMessage;
$scope.page.searchMessage = data.searchMessage;
} else {
$scope.displayMessageToUser = false;
}
}