2016-03-31 4 views
0

JAX-RS WebServiceコンテキストでManyToOneリレーションオブジェクトをシリアル化すると、Jsonでいくつかの問題を解決できますか?
役割エンティティAngularJs ManyToOne Json

@Entity 
@XmlRootElement 
@Entity 
public class Role implements ... 
{ 
    @JsonManagedReference 
    private List<User> user = new ArrayList<User>();  
    ..... 
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "role") 
    public List<User2015> getUser2015s() { 
     return this.user2015s; 
    } 
} 

ユーザエンティティ

@Entity 
public class User implements ... 
{ 
    @JsonBackReference 
    private Role role; 
    ..... 
    @ManyToOne(fetch = FetchType.EAGER) 
    @JoinColumn(name = "Role_Id", nullable = false) 
    public Role getRole() { 
     return this.role; 
    } 

} 

私は解決するために、ジャクソンアノテーションを使用: 私はそれぞれOneToManyとManyToOne、聖霊降臨祭のHibernateアノテーションでリンクされ2つのテーブルの役割とユーザーを持っていますjsonマッピングで無限に再帰的にオーバーフローする

残りのエンドポイントの役割

@GET 
@Produces("application/json") 
public List<User> listAll(@QueryParam("start") Integer startPosition, 
      @QueryParam("max") Integer maxResult) { 
    TypedQuery<User2015> findAllQuery = em 
      .createQuery(
      "<b>SELECT DISTINCT u FROM User u LEFT JOIN FETCH u.role ORDER BY u.id</b>", 
.... 
    final List<User> results = findAllQuery.getResultList(); 


return results; 
} 

及び方法listAllの():として残りのエンドポイントユーザに背の高い()メソッド

@GET 
@Produces("application/json") 
public List<Role> listAll(@QueryParam("start") Integer startPosition, @QueryParam("max") Integer maxResult) { 
    TypedQuery<Role> findAllQuery = em.createQuery(
    "<b>SELECT DISTINCT r FROM Role r LEFT JOIN FETCH r.users ORDER BY r.id", Role.class</b>); 
....... 
    if(results != null) 
     return results; 
    else 
     return null; 
} 

、すべて今までもそうです...返さ検査結果各リスト要素にはすべてのオブジェクトが設定されています(各ユーザーにはロールオブジェクトが入力されており、その逆もあります)

のangularjs工場$リソース

工場出荷時のユーザー

angular.module('backend').factory('UserResource', function($resource){ 
var resource = $resource('rest/users/:Userd',{UserId:'@id'}, 
         {'queryAll':{method:'GET',isArray:true}, 
         'query':{method:'GET',isArray:false}, 
         'update':{method:'PUT'}}); 
return resource; 

});

工場役割

angular.module('backend').factory('RoleResource', function($resource){ 
var resource = $resource('rest/roles/:RoleId',{RoleId:'@id'}, 
         {'queryAll':{method:'GET',isArray:true}, 
         'query':{method:'GET',isArray:false}, 
         'update':{method:'PUT'}}); 
return resource; 

})。残りの役割queryallを起動

は()JSONが期待右のようである返さ:

[{"id":1,"codice":"POS","descrizione":"Point Of Sale",<b>"users"</b>:[{"id":1,"username":"doe","codice":"30030","isEnable":false,"dataCreazione":1 ... 

それぞれ役割が(多くのユーザー

を持っているが、他の側に残り、ユーザーqueryallを呼び出す)JSONが欠落しているロールオブジェクト情報を生成します

[{"id":1,"username":"doe","codice":"30030mella","isEnable":false,"dataCreazione":1450738800000,"idReale":1,"dataExpire":"2099-12-31"}, 
{"id":2,"username":"foo","codice" .... 

私が期待します(と私が必要)役割の情報を持っていますユーザーごとに

いくつかの提案は非常に感謝しています。 私は何が間違っていますか?

答えて

0

@JsonManagedReference/@JsonBackReferenceこれはできません。

は、Jackson 2.0、環状オブジェクトグラフの処理前( より一般的または、共有参照のこと)が 親/子(双方向)の参照の特定の場合に限定されていました。

それはクラスで使用することができます

(そのタイプの特性は、機能が有効になっている必要があることを示すために)ならびに個々の特性上(支持体へ:

は、代替@JsonIdentityInfoがあまりにも提示されhttp://wiki.fasterxml.com/JacksonFeatureObjectIdentityを見ますタイプ自体に注釈を付けることができない場合、または異なるID生成シーケンスを使用する場合)。

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id") 
 
public class User { 
 

 
    private Role role; 
 

 
    public Role getRole() { 
 
     return this.role; 
 
    } 
 
} 
 

 
////// 
 

 
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id") 
 
public class Role { 
 

 
    private List<User> user = new ArrayList<>(); 
 

 
    public List<User> getUser() { 
 
     return user; 
 
    } 
 
}