2017-02-21 15 views
2

Spring Data JPAで使用されるSpring Data Restに問題があります。私はSpringブート1.4.4.RELEASEを使用しています。ここでSpringデータの残りとjpa @OneToManyは "_links"を複製します

は私の春・データ・休息のリポジトリである:ここでは

public interface ProfileJpaRepository extends JpaRepository<Profile, Long> { 
} 

は私のエンティティ(ゲッターとセッター簡潔にするために示されていない)です。エンティティを作成するために

@Entity 
@Table(name = "PROFILE") 
public class Profile { 

    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private Long id; 

    private String description; 

    // Don't use mappedBy="profile" because attributes are not persisted properly 
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) 
    @JoinColumn(name = "PROFILE_ID") 
    private Set<Attribute> attributes; 

    ... 
} 

Attribute.java

@Entity 
@Table(name = "ATTRIBUTE") 
public class Attribute { 

    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    @Column(name = "ID") 
    private Long id; 

    private String uri; 

    @ManyToOne(fetch = FetchType.EAGER) 
    private Profile profile; 

    @ElementCollection(fetch = FetchType.EAGER) 
    @CollectionTable(name="ATTRIBUTE_DATAS") 
    private List<String> datas = new ArrayList<>(); 

    public Attribute() {} 

    public Attribute(String uri, List<String> datas) { 
     this.uri = uri; 
     this.datas = datas; 
    } 

    ... 
} 

POST "http://localhost:8880/profiles" 上:

Profile.java I

{ 
    "description" : "description-value", 
    "attributes" : [ 
     { 
      "uri" : "uri-a", 
      "datas" : ["uri-a-value"] 
     }, 
     { 
      "uri" : "uri-b", 
      "datas" : ["uri-b-value"] 
     } 
    ] 
} 

そして、ここではその結果でありますヒットhttp://localhost:8880/profiles

{ 
    "_embedded" : { 
    "profiles" : [ { 
     "description" : "description-value", 
     "attributes" : [ { 
     "uri" : "uri-a", 
     "datas" : [ "uri-a-value" ], 
     "_links" : { 
      "profile" : { 
      "href" : "http://localhost:8880/profiles/1" 
      } 
     } 
     }, { 
     "uri" : "uri-b", 
     "datas" : [ "uri-b-value" ], 
     "_links" : { 
      "profile" : { 
      "href" : "http://localhost:8880/profiles/1" 
      } 
     } 
     } ], 
     "_links" : { 
     "self" : { 
      "href" : "http://localhost:8880/profiles/1" 
     }, 
     "profile" : { 
      "href" : "http://localhost:8880/profiles/1" 
     } 
     } 
    } ] 
    }, 
    "_links" : { 
    "self" : { 
     "href" : "http://localhost:8880/profiles" 
    }, 
    "profile" : { 
     "href" : "http://localhost:8880/profile/profiles" 
    } 
    }, 
    "page" : { 
    "size" : 20, 
    "totalElements" : 1, 
    "totalPages" : 1, 
    "number" : 0 
    } 
} 

すべての属性の下で"_links"が指定されているため、問題があると思います。私はJpaRepositoryにMongoRepositoryからの切り替え、およびMongoRepositoryを使用している

{ 
    "_embedded" : { 
    "profiles" : [ { 
     "description" : "description-value", 
     "attributes" : [ { 
     "uri" : "uri-a", 
     "datas" : [ "uri-a-value" ] 
     }, { 
     "uri" : "uri-b", 
     "datas" : [ "uri-b-value" ] 
     } ], 
     "_links" : { 
     "self" : { 
      "href" : "http://localhost:8880/profiles/1" 
     }, 
     "profile" : { 
      "href" : "http://localhost:8880/profiles/1" 
     } 
     } 
    } ] 
    }, 
    "_links" : { 
    "self" : { 
     "href" : "http://localhost:8880/profiles" 
    }, 
    "profile" : { 
     "href" : "http://localhost:8880/profile/profiles" 
    } 
    }, 
    "page" : { 
    "size" : 20, 
    "totalElements" : 1, 
    "totalPages" : 1, 
    "number" : 0 
    } 
} 

注、これら"_links"が「重複」されませんでした。むしろ、私はこのような何かを期待しているだろう。

誰かがこれにいくつかの光を当てることができますか? JPAエンティティで何かを間違って構成しましたか?残りのリポジトリに何かを設定する必要はありますか?依存関係のバージョンについて

詳しい情報はここで見つけることができます、あなたがそれを必要とする必要があり、私はこれらの(http://docs.spring.io/spring-boot/docs/1.4.4.RELEASE/reference/html/appendix-dependency-versions.html

感謝を上書きしませんでした。

+0

をあなたはどのように/なぜ_links知っていますかあなたのコードを生成されますか?私はJSON-Serialization中にそれが起こったと仮定していますが、私はそれらをデフォルト設定で使用したことはありません。 –

+2

@トルステンN。これはProfileJpaRepositoryによって自動的に行われ、Spring Data RestはJpaRepositoryを拡張し、自動的にメディアタイプとしてHALを使用してドメインモデルのための発見可能なREST APIを公開します。 – Nis

答えて

0

私は問題を解決するために、プロジェクション(詳細はこちら:http://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts)を使用しています。投稿時にその機能を認識できませんでした。

import com.fasterxml.jackson.annotation.JsonIgnore; 

@Entity 
@Table(name = "ATTRIBUTE") 
public class Attribute { 
    ... 

    @ManyToOne(fetch = FetchType.EAGER) 
    @JsonIgnore 
    private Profile profile; 

    ... 
} 

そしてInlineAttributes投影:私は@JsonIgnoreアノテーションを追加する必要がありましたAttributeクラスで

import org.springframework.data.rest.core.annotation.RepositoryRestResource; 

@RepositoryRestResource(excerptProjection = InlineAttributes.class) 
public interface ProfileJpaRepository extends JpaRepository<Profile, Long> { 
} 

私は自分のリポジトリに注釈を付け、私のInlineAttributes投影を使用することを教えなければなりませんでした私はクラスを作成しなければなりませんでした。

import org.springframework.data.rest.core.config.Projection; 
import com.fasterxml.jackson.annotation.JsonPropertyOrder;  

@Projection(name = "inlineAttributes", types = { Profile.class }) 
@JsonPropertyOrder({ "description", "attributes" }) 
public interface InlineAttributes { 

    public String getDescription(); 
    public Set<Attribute> getAttributes(); 
} 

そして、あなたは残りのエンドポイント呼び出しているときの投影を適用する必要があります:それは以前と同じではなかったように私は順序を指定

http://localhost:8880/profiles?projection=inlineAttributes 
関連する問題