指向Spring Data Rest中嵌入实体的链接
我在项目中定义了以下实体: 国家指向Spring Data Rest中嵌入实体的链接,spring,rest,spring-boot,spring-data-jpa,spring-data-rest,Spring,Rest,Spring Boot,Spring Data Jpa,Spring Data Rest,我在项目中定义了以下实体: 国家 @Entity @Data public class Country { @Id @GeneratedValue(strategy = GenerationType.AUTO) Long id; @Column(nullable = false) String name; @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) List&
@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>();
}
@Entity
@Data
public class City {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@Column(nullable = false)
String name;
@ManyToOne
Country country;
}
人
@Entity
@Data
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@Column
String name;
@Embedded
Address address = new Address();
}
地址
@Data
public class Address {
@Column
String line;
@ManyToOne
Country country;
@ManyToOne
City city;
}
我还为个人
、国家
和城市
定义了存储库
当我向/persons/1发出GET请求时,我得到以下结果:
{
"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"
}
}
}
我怀疑,由于地址是一个嵌入对象,生成的指向国家和城市的链接是错误的。尽管存在城市
和国家
值,但它们不会返回任何内容。正确的链接应该是什么
Spring Data Rest不支持嵌入式对象吗?可能的解决方案:
- 将关联移动到父实体
- 将可嵌入资源升级为单独的实体资源
- 添加
以删除这些链接ResourceProcessor
- 添加自定义控制器以处理这些链接
更新:这似乎已经在SpringDataRESTV2.1中修复。请参阅。我认为您无法访问
/persons/1/地址
?它返回HTTP 400错误请求,并显示以下消息:PersistentEntity不能为null!