Having following Entities and Repositories. I could'nt manage to put the id on my relationship. Thanks in advance for your help
Related artifacts from my build.gradle (using Spring Boot version 1.5.4.RELEASE
)
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-rest')
Entities
Store
@Entity
@Table(name = "store")
class Store {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
long id
String description
@ManyToOne(optional=false, fetch = FetchType.EAGER)
Province province
}
Province
@Entity
@Table(name = "province")
class Province {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
long id
@NotNull
String name
}
Repositories
Store
@RepositoryRestResource(collectionResourceRel = "stores", path = "stores")
interface StoreRepository extends PagingAndSortingRepository<Store, Long> {
}
Province
@RepositoryRestResource(collectionResourceRel = "provinces", path = "provinces")
interface ProvinceRepository extends PagingAndSortingRepository<Province, Long> {
}
Expected Result I'm expecting this result, please note link on Province
{
"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
}
Actual Result Not having the Province Id in 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
}
Basically im expecting
this
"province": {
"href": "http://localhost:8080/stores/1/province/1" ==> Expecting the url with provinceID since its many to one
}
instead of this
"province": {
"href": "http://localhost:8080/stores/1/province" //NO province ID
}
Edit Jun 21 at 11:54 I've changed FetchType.LAZY
to EAGER
on Store
due to an error when trying to do
"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
Finally i've found two possibles ways to solve this one with Projections and the other using ResourceProcessor . Hope this help somebody.
@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 result
{
//Store data...
"provinceId": "1"
"provinceName": "Prov1"
//Links,etc data
}
ResourceProcessor
in order to add a new link with desired info
@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;
}
};
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.