resource link rel with id spring data rest
resource link rel with id spring data rest
拥有以下实体和存储库。我无法在我的关系上加上身份。预先感谢您的帮助
来自我的 build.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> {
}
预期结果
我期待这个结果,请注意link省
{
"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
中没有省份 ID
{
"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
由于尝试执行
时出错,我已将 Store
上的 FetchType.LAZY
更改为 EAGER
"http://localhost:8080/stores/1/province/1"
得到
"org.springframework.http.converter.HttpMessageNotWritableException",
"message": "Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer
最后我找到了两种可能的方法来解决这个问题,一个是 Projections,另一个是 ResourceProcessor。希望这对某人有所帮助。
@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
以添加具有所需信息的新 link
@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;
}
};
}
}
拥有以下实体和存储库。我无法在我的关系上加上身份。预先感谢您的帮助
来自我的 build.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> {
}
预期结果 我期待这个结果,请注意link省
{
"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
中没有省份 ID{
"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 由于尝试执行
时出错,我已将Store
上的 FetchType.LAZY
更改为 EAGER
"http://localhost:8080/stores/1/province/1"
得到
"org.springframework.http.converter.HttpMessageNotWritableException",
"message": "Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer
最后我找到了两种可能的方法来解决这个问题,一个是 Projections,另一个是 ResourceProcessor。希望这对某人有所帮助。
@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
以添加具有所需信息的新 link
@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;
}
};
}
}