我试图在Spring Rest Controller中使用PUT请求方法部分更新实体时,区分空值和未提供的值.
以下面的实体为例:
@Entity
private class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/* let's assume the following attributes may be null */
private String firstName;
private String lastName;
/* getters and setters ... */
}
Run Code Online (Sandbox Code Playgroud)
我的人员库(Spring Data):
@Repository
public interface PersonRepository extends CrudRepository<Person, Long> {
}
Run Code Online (Sandbox Code Playgroud)
我使用的DTO:
private class PersonDTO {
private String firstName;
private String lastName;
/* getters and setters ... */
}
Run Code Online (Sandbox Code Playgroud)
我的Spring RestController:
@RestController
@RequestMapping("/api/people")
public class PersonController {
@Autowired
private PersonRepository people; …Run Code Online (Sandbox Code Playgroud)