Java EE Webアプリケーションの開発は初めてで、ブラウザまたはスマートフォンを介して登録およびログインできる単純なコンポーネントを開発しようとしています。私は netbeans 7.3 と glassfish サーバーを使用しています。いくつかの Web サービスを実装する必要がありますが、Java でそれを実行した経験はありません。
最初に、JavaServletをコントローラーとして、JPAページをビューとして、EJBクラスをモデルとして使用して、通常のWebページを作成しました。問題なく、すべて正常に動作します。
しかし今、JSON オブジェクトを使用して前述の Web サービスを実装しようとしています。最初は、NetBeans 関数を使用してエンティティから対応するクラスを生成しました。「RESTful Web サービスのテスト」機能でテストすると、netbeans は、実装されているすべての Web サービスを試すことができるページを生成します。アプリケーション/XML GET/POST メソッドを試すと、正常に動作します。しかし、アプリケーション/JSON に切り替えると、何か問題が発生します。
たとえば、ユーザーの XML 表現として結果を期待して getUserByID(int id) を呼び出すと、関数は適切な結果を返します。
<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":
... Cycling goupsCollection (グループ コレクションは、ユーザーが所属するグループのリストです。
私の User モデルは次のように定義されています。
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 + " ]" ;
}
}