2017-06-20 96 views
0

具有以下實體和存儲庫。我無法設法把我的關係放在身份證上。在此先感謝您的幫助資源鏈接rel與ID彈簧數據休息

從我的構建相關artifacts。gradle這個(使用Spring引導版本1.5.4.RELEASE

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> { 
} 

預期結果 我期待這樣的結果,請注意鏈接省

{ 
"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 
} 

實際結果 不具有省標識在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 
} 

基本上我期待

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

,而不是這個

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

編輯6月21日在11:54 我已經改變了FetchType.LAZYEAGERStore因t時出錯rying做

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

GOT

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

該問題可能是@ManyToOne,因爲鏈接存儲在Province表中而不是Store表。但我很誠實:我從來沒有見過這個問題。 – benkuly

回答

0

最後我已經發現了兩個候選條件的方式來解決這一使用ResourceProcessor預測和其他。希望這有助於某人。

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; 
      } 
     }; 
    } 
}