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.LAZY
到EAGER
上Store
因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
该问题可能是@ManyToOne,因为链接存储在Province表中而不是Store表。但我很诚实:我从来没有见过这个问题。 – benkuly