【发布时间】:2021-03-16 08:48:58
【问题描述】:
您好,我有带有 mongodb 的 springboot (spring-boot-starter-data-mongodb)
我的问题是如果我只发送一个或只发送我想要更改的字段,那么其他值设置为空。我在互联网上发现了一些东西,比如@DynamicUpdate,但不能在 mongodb 上工作,你能帮我解决这个问题吗?我是初学者,我不知道如何提供帮助,这对我来说很重要,如果您需要更多代码或更多信息,我会写在评论中。我希望我已经充分描述了这个问题。 :)
我的 POJO:
@Data
@Getter
@Setter
@NoArgsConstructor
public class Person {
@Id
private String id;
private String firstName;
private String lastName;
private boolean enabled;
private String note;
回购
@Repository
public interface PersonRepository extends MongoRepository <Person, String> {
}
我有这个电话
@PutMapping("/{id}")
@ResponseBody
public void UpdatePerson (@PathVariable String id , @RequestBody Person person) {
personRepository.save(person);
}
@GetMapping(path = "/{id}")
public Person getPersonByid(@PathVariable String id ){
return personRepository.findById(id).orElseThrow(PersonNotFound::new);
}
样本:
更新前打电话:
{
"id": "5fc940dc6d368377561dbb02",
"firstName": "Rambo",
"lastName": "Norris",
"enabled": true,
"note": "hello this is my first note from you",
}
拨打电话:
{
"id": "5fc940dc6d368377561dbb02",
"firstName": "Chuck"
}
更新后接电话:
{
"id": "5fc940dc6d368377561dbb02",
"firstName": "Chuck",
"lastName": null,
"enabled": false,
"note": null,
}
我想要什么
更新前打电话:
{
"id": "5fc940dc6d368377561dbb02",
"firstName": "Rambo",
"lastName": "Norris",
"enabled": true,
"note": "hello this is my first note from you",
}
拨打电话:
{
"id": "5fc940dc6d368377561dbb02",
"firstName": "Chuck"
}
更新后接电话:
{
"id": "5fc940dc6d368377561dbb02",
"firstName": "Chuck",
"lastName": "Norris",
"enabled": true,
"note": "hello this is my first note from you",
}
【问题讨论】:
标签: spring mongodb spring-boot upgrade mongorepository