I have the following entities defined in my project:
Country
@Entity
@Data
public class Country {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@Column(nullable = false)
String name;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
List<City> cities = new ArrayList<City>();
}
City
@Entity
@Data
public class City {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@Column(nullable = false)
String name;
@ManyToOne
Country country;
}
Person
@Entity
@Data
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@Column
String name;
@Embedded
Address address = new Address();
}
Address
@Data
public class Address {
@Column
String line;
@ManyToOne
Country country;
@ManyToOne
City city;
}
I have also repositories defined for Person
, Country
and City
.
When I make a GET request to /persons/1 I get the following result:
{
"name":null,
"address":{
"line":"Address1"
},
"_links":{
"self":{
"href":"http://localhost:8080/persons/1"
},
"city":{
"href":"http://localhost:8080/persons/1/city"
},
"country":{
"href":"http://localhost:8080/persons/1/country"
}
}
}
I suspect that since address is an embedded object, the generated links to country and city are wrong. They don't return anything although city
and country
values are present. What should the correct links be?
Are embedded objects not supported by Spring Data Rest?
CrudRepository provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.
It is not recommended in real-world applications as you are exposing your database entities directly as REST Services. While designing RESTful services, the two most important things that we consider are the domain model and the consumers. But, while using Spring Data REST, none of these parameters are considered.
@RepositoryRestResourceIf you want to write a custom handler for a specific resource taking advantage of Spring Data REST's settings, message converters, exception handling and more, you can use @RepositoryRestController (instead of the standard Spring MVC @Controller or @RestController annotations).
This is an interface that allows you to perform various operations with WebsiteUser objects. We also defined a custom query that will provide a list of users based on a given name. The @RepositoryRestResource annotation is optional and is used to customize the REST endpoint.
Possible solutions:
ResourceProcessor
to remove those linksUPDATE: This seems to be already fixed in Spring-DATA-REST v2.1. See DATAREST-262.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With