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

时间:2016-05-07 14:42:13

标签: javascript angularjs spring

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

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

2 个答案:

答案 0 :(得分:1)

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

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

    返回createListAllResponse(page,locale);

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

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

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

答案 1 :(得分:0)

感谢Artem Larin,

我可以访问服务器端的用户信息,但是当Response实体发送到angularjs端时,object不包含用户信息。

我用ArticleDTO对象解决了这种情况,该对象包含String userName aganist User对象。谢谢你的帮助。