nathan
09/06/2020, 6:53 PM//Controller
@PatchMapping("/comment/{id}/")
fun PatchCommentById(@PathVariable id: Long, @RequestBody comment: Comment): ResponseEntity<Comment> {
val updatedComment = repo.save(Comment(
id = id,
body = comment.body,
video_link = comment.video_link,
image_link = comment.image_link,
))
return ResponseEntity.ok(updatedComment)
}
how would u partial update this?AlexJuca
09/06/2020, 8:55 PMval updatedComment = Comment()
updatedComment.body = comment.body.let { it }
updatedComment.video_link = comment.video_link.let { it }
nathan
09/06/2020, 10:56 PMthanksforallthefish
09/07/2020, 6:17 AMval comment = repo.getById(id).apply {
body = comment.body
...
}
return ResponseEntity.ok(repo.save(comment))
there is also some sorcery I am never sure about, but if you in a @Transactional
repo.save
might happen implicitly, so you would not need to write the code yourself.thanksforallthefish
09/07/2020, 6:21 AMPATCH
is that simplistic. from the rfc (https://tools.ietf.org/html/rfc5789#section-2)
With PATCH, however, the enclosed entity contains a set of instructions describing how a resource currently residing on the origin server should be modified to produce a new version.
then json-patch was created with more specs on the operation part: https://tools.ietf.org/html/rfc6902#page-4AlexJuca
09/07/2020, 7:37 AMChris Voy
09/07/2020, 9:48 AMclass CommentRequest(
val body: JsonNullable<String> = JsonNullable.undefined(),
// etc.
)
with the help of https://github.com/OpenAPITools/jackson-databind-nullable.
You can then do this in your service:
if (commentRequest.body.isPresent()) {
comment.body = commentRequest.body
}
// etc.