MongoDB 和 Spring MVC:PATCH 操作?
MongoDB and Spring MVC: PATCH operation?
我有以下带有 Spring Web
框架的简单 Java
控制器:
@RestController
@RequestMapping("/rounds")
@Slf4j
public class RoundController {
private RoundService roundService;
@Autowired
public RoundController(RoundService roundService) {
this.roundService = roundService;
}
@GetMapping(
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public List<Round> find() {
return roundService.find();
}
@GetMapping(
path = "/{userId}",
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public List<Round> get(@PathVariable String userId) {
return roundService.getRoundsByUserId(userId);
}
@PostMapping(
produces = MediaType.APPLICATION_JSON_VALUE
)
@ResponseStatus(HttpStatus.CREATED)
public Round create(@Valid @NotNull @RequestBody Round round) {
roundService.create(round);
return round;
}
@DeleteMapping(
path = "/{id}",
produces = MediaType.APPLICATION_JSON_VALUE
)
@ResponseStatus(HttpStatus.OK)
public void delete(@PathVariable String id) {
ObjectId objectId = new ObjectId(id);
roundService.delete(objectId);
}
}
在使用 Mongo
时,是否有针对对象进行更新/修补的最佳实践?
是否最好只使用 POST
方法,并使用用户所做的更改将 Round 对象重新保存在数据库中?
根据我的说法,最佳做法不应该是使用 POST 来执行 update/patch。
让你POST只做Round创作。
如果您使用 spring 数据 mongodb 只需使用您的实体调用存储库的保存方法
见 https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/repository/MongoRepository.html
对于更新,最好在您的控制器中添加 PUT /{roundId} 并且:
- 如果你拥有所有回合数据,请调用你的保存方法
- 调用 findById 获取完整数据并设置要更改的数据,然后保存(但这更像是 PATCH)
或者您也可以添加 PATCH /{roundId} 并仅更新文档中您想要的字段
见 https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/MongoTemplate.html
我有以下带有 Spring Web
框架的简单 Java
控制器:
@RestController
@RequestMapping("/rounds")
@Slf4j
public class RoundController {
private RoundService roundService;
@Autowired
public RoundController(RoundService roundService) {
this.roundService = roundService;
}
@GetMapping(
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public List<Round> find() {
return roundService.find();
}
@GetMapping(
path = "/{userId}",
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public List<Round> get(@PathVariable String userId) {
return roundService.getRoundsByUserId(userId);
}
@PostMapping(
produces = MediaType.APPLICATION_JSON_VALUE
)
@ResponseStatus(HttpStatus.CREATED)
public Round create(@Valid @NotNull @RequestBody Round round) {
roundService.create(round);
return round;
}
@DeleteMapping(
path = "/{id}",
produces = MediaType.APPLICATION_JSON_VALUE
)
@ResponseStatus(HttpStatus.OK)
public void delete(@PathVariable String id) {
ObjectId objectId = new ObjectId(id);
roundService.delete(objectId);
}
}
在使用 Mongo
时,是否有针对对象进行更新/修补的最佳实践?
是否最好只使用 POST
方法,并使用用户所做的更改将 Round 对象重新保存在数据库中?
根据我的说法,最佳做法不应该是使用 POST 来执行 update/patch。
让你POST只做Round创作。
如果您使用 spring 数据 mongodb 只需使用您的实体调用存储库的保存方法 见 https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/repository/MongoRepository.html
对于更新,最好在您的控制器中添加 PUT /{roundId} 并且:
- 如果你拥有所有回合数据,请调用你的保存方法
- 调用 findById 获取完整数据并设置要更改的数据,然后保存(但这更像是 PATCH)
或者您也可以添加 PATCH /{roundId} 并仅更新文档中您想要的字段 见 https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/MongoTemplate.html