gpt4 book ai didi

javascript - 如何通过Spring Rest和Angularjs获取Entity的对象信息?

转载 作者:行者123 更新时间:2023-12-03 07:00:18 24 4
gpt4 key购买 nike

我有一个请求处理程序方法“listAll”,它返回所有文章。当我调用此方法时,它返回文章列表,但是当我在 angularjs 端访问此返回时,它不包含用户信息,例如article.user.name 不返回任何信息,但我可以访问其他文章字段。我的实体、AngularJS 脚本和 Controller 如下。感谢您的帮助

@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;
}
}

请求处理程序方法:

  @RequestMapping(method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<?> listAll(@RequestParam int page, Locale locale) {
return createListAllResponse(page, locale);
}

Angularjs 脚本:

它返回“data.articles[i].user”在以下脚本中未定义:

$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;
}
}

最佳答案

由于您没有向我们提供数据库架构,我有两个假设:

  1. 首先,确保服务器端的Article.User字段不为空。在以下行设置断点:

    返回createListAllResponse(页面,区域设置);

    到达断点后,按 Alt+F8(在 IDEA 中)以查看响应的值。

  2. 您是否尝试过添加@JoinColumn?

@ManyToOne
@JoinColumn(name="USER_ID")
private User user;

关于javascript - 如何通过Spring Rest和Angularjs获取Entity的对象信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37089856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com