2017-06-20 1 views
0

以下のエンティティとリポジトリを持つ。私は私の関係にIDを置くことができなかった。あなたの助けを前もってありがとうリソースリンクrel with idスプリングデータrest

私のビルドからの関連する人工物。(春のブートバージョン1.5.4.RELEASEを使用して)のGradle

compile('org.springframework.boot:spring-boot-starter-data-jpa') 
compile('org.springframework.boot:spring-boot-starter-data-rest') 

エンティティ

ストア

@Entity 
@Table(name = "store") 
class Store { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    long id 

    String description 

    @ManyToOne(optional=false, fetch = FetchType.EAGER) 
    Province province 
} 

@Entity 
@Table(name = "province") 
class Province { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    long id 

    @NotNull 
    String name 
} 

リポジトリ

ストア

@RepositoryRestResource(collectionResourceRel = "stores", path = "stores") 
interface StoreRepository extends PagingAndSortingRepository<Store, Long> { 
} 

@RepositoryRestResource(collectionResourceRel = "provinces", path = "provinces") 
interface ProvinceRepository extends PagingAndSortingRepository<Province, Long> { 
} 

私はこの結果を期待してい 、基本的に


のhref

{ 
"stores": [ 
    { 
    "id": 1, 
    "description": "desc1", 
    "_links": { 
     "self": { 
     "href": "http://localhost:8080/stores/1" 
     }, 
     "store": { 
     "href": "http://localhost:8080/stores/1" 
     }, 
     "province": { 
     "href": "http://localhost:8080/stores/1/province" ==> //NO ID! 
     } 
    } 
    } 
] 
    //Simplified for simplicity sake 
} 

{ 
"stores": [ 
    { 
    "id": 1, 
    "description": "desc1", 
    "_links": { 
     "self": { 
     "href": "http://localhost:8080/stores/1" 
     }, 
     "store": { 
     "href": "http://localhost:8080/stores/1" 
     }, 
     "province": { 
     "href": "http://localhost:8080/stores/1/province/1" ==> Expecting the url with provinceID since its many to one 
     } 
    } 
    } 
] 
    //Simplified for simplicity sake 
} 

の実績 持っていない省同上省にリンクを注意してください結果予想予期している

この

"province": { 
      "href": "http://localhost:8080/stores/1/province/1" ==> Expecting the url with provinceID since its many to one 
      } 

の代わりに、11時54分 でこの

"province": { 
    "href": "http://localhost:8080/stores/1/province" //NO province ID 
    } 

編集6月21日私はのためにStoreEAGERFetchType.LAZYを変更しましたt

"http://localhost:8080/stores/1/province/1" 

を行うためにrying

"org.springframework.http.converter.HttpMessageNotWritableException", 
    "message": "Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer 
+0

リンクがProvinceテーブルに格納されていてストアテーブルに格納されていないため、この問題は@ManyToOneである可能性があります。しかし、私は正直です:私は以前この問題を見たことがない。 – benkuly

答えて

0

は最終的に私はResourceProcessorを使用してプロジェクションや他でこれを解決するには、2つのpossiblesの方法を見つけたました。これは誰かを助けることを願っています。

Projections

@Projection(name="storeProjection", types={Store.class}) 
interface StoreProjection { 

    long getId(); 
    String getDescription(); 
    String getPhoneNumber(); 
    StoreStatus getStatus(); 

    @Value("#{target.province.id}") 
    Long getProvinceId(); 

    @Value("#{target.province.name}") 
    String getProvinceName(); 
} 

GET http://localhost:8080/stores?projection=storeProjection

JSON結果希望情報で新しいリンクを追加するために、

{ 
    //Store data... 

    "provinceId": "1" 
    "provinceName": "Prov1" 

//Links,etc data 
} 

ResourceProcessor

@Configuration 
class ResourcesProcessors { 

    @Autowired 
    EntityLinks entityLinks 

    @Bean 
    public ResourceProcessor<Resource<Store>> storeProcessor() { 

     return new ResourceProcessor<Resource<Store>>() { 
      @Override 
      public Resource<Store> process(Resource<Store> resource) { //Este punto solo se agregan nuevos links 

       Store store = resource.getContent() 
       Link link = entityLinks.linkToSingleResource(Province.class, store.province.id); 
       resource.add(link); 
       return resource; 
      } 
     }; 
    } 
} 
関連する問題