0

我是开发 Java EE Web 应用程序的新手,我正在尝试开发简单的组件,允许人们在浏览器或智能手机中注册和登录。我使用 netbeans 7.3 和 glassfish 服务器。我需要实现一些 Web 服务,但我以前没有任何在 Java 中这样做的经验。

首先我做了普通的网页,使用 JavaServlet 作为控制器,JPA 页面作为视图,EJB 类作为模型。一切正常,没有任何问题。

但现在我正在尝试使用 JSON 对象来实现提到的 Web 服务。一开始我使用 NetBeans 函数从实体生成相应的类。使用“测试 RESTful Web 服务”功能进行测试时,netbeans 会生成页面,我可以在其中尝试所有已实现的 Web 服务。当我尝试应用程序/XML GET/POST 方法时,它们工作正常。但是当我切换到应用程序/JSON 时,出现了问题。

例如,当我调用 getUserByID(int id) 期望结果作为用户的 XML 表示时,函数返回良好的结果:

<user> 
  <groupsCollection> 
    <groupName>administrator</groupName> 
    <id>1</id> 
  </groupsCollection> 
  <id>1</id> 
  <password>21232f297a57a5a743894a0e4a801fc3</password> 
  <username>admin</username> 
</user> 

但是对预期结果做同样的事情,因为 JSON 对象会导致循环:

{"id":1,"username":"admin","password":"21232f297a57a5a743894a0e4a801fc3",
"groupsCollection":[{"id":1,"groupName":"administrator","userCollection":[{"groupsCollection":[{"id":1,"groupName":"administrator","userCollection":[{"groupsCollection":[{"id":1,"groupName":"administrator","userCollection":[{"groupsCollection":[{"id":1,"groupName":"administrator","userCollection":[{"groupsCollection":[{"id":1,"groupName":"administrator","userCollection":[{"groupsCollection":[{"id":1,"groupName":"administrator","userCollection":[{"groupsCollection":[{"id":1,"groupName":"administrator","userCollection":[{"groupsCollection":[{"id":1,"groupName":"administrator","userCollection":[{"groupsCollection":[{"id":1,"groupName":"administrator","userCollection":[{"groupsCollection":[{"id":1,"groupName":"administrator","userCollection":[{"groupsCollection":[{"id":1,"groupName":"administrator","userCollection":[{"groupsCollection":[{"id":1,"groupName":"administrator","userCollection":[{"groupsCollection": 

... 循环 goupsCollection(组集合是用户所属组的列表。

我的用户模型定义为:

public class User implements Serializable {
    @ManyToMany(mappedBy = "userCollection")
    private Collection<Groups> groupsCollection = new HashSet();
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "username")
    private String username;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "password")
    private String password;

   constructors, geters, seters ...


    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }



    @Override
    public String toString() {
        return "entities.User[ id=" + id + " ]";
    }

    public Collection<Groups> getGroupsCollection() {
        return groupsCollection;
    }

    public void setGroupsCollection(Collection<Groups> groupsCollection) {
        this.groupsCollection = groupsCollection;
    }

    public void addGroup(Groups group) {
        this.groupsCollection.add(group); 
    }

} 

所以我的问题是,当期望它作为 JSON 对象时,我应该怎么做才能得到正确的结果。如果需要任何细节,请随时询问。感谢您的每一个回答。

更新:集团实体

@Entity
@Table(name = "groups")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Groups.findAll", query = "SELECT g FROM Groups g"),
    @NamedQuery(name = "Groups.findById", query = "SELECT g FROM Groups g WHERE g.id = :id"),
    @NamedQuery(name = "Groups.findByGroupName", query = "SELECT g FROM Groups g WHERE g.groupName = :groupName")})
public class Groups implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "group_name")
    private String groupName;
    @JoinTable(name = "users_groups", joinColumns = {
        @JoinColumn(name = "group_id", referencedColumnName = "id")}, inverseJoinColumns = {
        @JoinColumn(name = "user_id", referencedColumnName = "id")})
    @ManyToMany
    private Collection<User> userCollection = new HashSet();

    public Groups() {
    }

    public Groups(Integer id) {
        this.id = id;
    }

    public Groups(Integer id, String groupName) {
        this.id = id;
        this.groupName = groupName;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    @XmlTransient
    public Collection<User> getUserCollection() {
        return userCollection;
    }

    public void setUserCollection(Collection<User> userCollection) {
        this.userCollection = userCollection;
    }

    public void addUser(User user){
        this.userCollection.add(user);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Groups)) {
            return false;
        }
        Groups other = (Groups) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Groups[ id=" + id + " ]" ;
    }

}
4

0 回答 0