简体   繁体   English

在 RESTful Spring 引导应用程序中,如何对嵌套的 object 属性进行 url 查询?

[英]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 .在这里,我们使用以下类CustomerAddress 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.

相关问题 如何将嵌套的 JSON object 转换为阵列 Spring 引导? - How do i turn a nested JSON object to an array Spring boot? 如何在 Spring Boot 应用程序的 application.properties 文件中设置 MyBatis 配置属性? - How do I set MyBatis configuration properties in an application.properties file in a Spring Boot application? 在 Spring Boot 中,如何 URL 查询嵌入对象的属性? - In Spring Boot, how do you URL query properties of embedded objects? 如何使用Maven在现有Spring Boot应用程序中使用属性文件? - How do I use Maven to Utilize Properties File with Existing Spring Boot Application? 在用于spring boot之前,我如何首先在application.properties中解密密码 - How do I KMS decrypt the password in the application.properties first before being used in spring boot Spring-Boot:如何在@ImportResource中引用application.properties - Spring-Boot: How do I reference application.properties in an @ImportResource "如何在 application.properties 文件中的 Spring Boot 应用程序中配置 HikariCP?" - How do I configure HikariCP in my Spring Boot app in my application.properties files? 如何使用 Z2A2D595E6ED9A0B234D6B7 将 log4j.xml、应用程序属性和 jar 外部化? - How do I externalize log4j.xml,application properties and jar using spring boot? 如何使用配置类在 Spring Boot 中为我的应用程序配置属性? - How do I use a configuration class to configure properties for my application in Spring Boot? 如何使用Sping Data Jpa OneToMany嵌套模型在Spring Boot Restful API上执行CURD操作? - How to do the CURD operations on Spring Boot Restful API using Sping Data Jpa OneToMany nested model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM