[英]In a RESTful Spring Boot application, how do I do url queries of nested object properties?
Here, we are using the following classes, Customer
and Address
.在这里,我们使用以下类Customer
和Address
。 Assume getters and setters are implemented, and the constructors fill all fields except for the automatically filled id
:假设实现了 getter 和 setter,并且构造函数填充除了自动填充的id
之外的所有字段:
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
@OneToOne
private Address address;
/** ... */
}
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String street;
private String city;
private String state;
private String zipCode;
/** ... */
}
Notice how the field state
is nested inside of the Address
instance object inside of the Customer
class.请注意字段state
如何嵌套在Customer
class 内部的Address
实例 object 内。
Now our project implements the JpaRepository<Customer, Long>
interface in order to handle all queries, and said interface includes a findAll
function that takes an Example<Customer>
and searches the database according to that example.现在我们的项目实现了JpaRepository<Customer, Long>
接口以处理所有查询,并且所述接口包括一个findAll
function ,它采用Example<Customer>
并根据该示例搜索数据库。 So we set up our handler所以我们设置了我们的处理程序
@GetMapping
public ResponseEntity<List<Customer>> getCustomers(@Valid Customer customer) {
/** call to customerRepository.findAll(Example.of(customer)) inside here */
}
Now in our RESTful application, we type in the URL /customers?state=IL
, expecting the customer
argument to be fed现在在我们的 RESTful 应用程序中,我们输入 URL /customers?state=IL
,期望输入customer
参数
new Customer(null, null, new Address(null, null, "IL", null))
but instead, it is fed但相反,它被喂食
new Customer(null, null, null)
Why is that, and what do we have to do to make sure the first object is passed instead of the second?为什么会这样,我们必须做些什么来确保通过第一个 object 而不是第二个?
The easiest way to solve this is to use the @Embedded and @Embeddable annotations.解决此问题的最简单方法是使用 @Embedded 和 @Embeddable 注释。
@Entity
public class Customer{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
@Embedded
private Address address;
}
@Entity
@Embeddable
public class Address{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String street;
private String city;
private String state;
private String zipCode
}
This will solve your problem, @Embedded will embed Address into Customer Entity hence solve your problem.这将解决您的问题,@Embedded 会将地址嵌入到客户实体中,从而解决您的问题。
If you use '@Valid Customer customer' and when you did '/customers?state=IL'no Adress instance will be created automatically.如果您使用 '@Valid Customer customer' 并且当您使用 '/customers?state=IL' 时,将不会自动创建地址实例。
Because your request wait for a body.因为你的请求等待一个body。 Here if you want init a address you should put @param(´state') String state and instanciate manually.在这里,如果你想初始化一个地址,你应该输入 @param(´state') String state 并手动实例化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.