From 26e179e3d3f321c8b951c65d67c1bf8d08538adb Mon Sep 17 00:00:00 2001 From: kwoyke Date: Wed, 29 Dec 2021 13:50:16 +0100 Subject: [PATCH] BAEL-5313: Use id path variable in the PUT mapping (#11634) --- .../springpagination/controller/PostRestController.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/spring-boot-rest/src/main/java/com/baeldung/springpagination/controller/PostRestController.java b/spring-boot-rest/src/main/java/com/baeldung/springpagination/controller/PostRestController.java index c52960b365..500de17e1e 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/springpagination/controller/PostRestController.java +++ b/spring-boot-rest/src/main/java/com/baeldung/springpagination/controller/PostRestController.java @@ -19,6 +19,7 @@ import org.springframework.web.bind.annotation.ResponseStatus; import java.text.ParseException; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; @Controller @@ -65,7 +66,11 @@ public class PostRestController { @PutMapping(value = "/{id}") @ResponseStatus(HttpStatus.OK) - public void updatePost(@RequestBody PostDto postDto) throws ParseException { + public void updatePost(@PathVariable("id") Long id, @RequestBody PostDto postDto) throws ParseException { + if(!Objects.equals(id, postDto.getId())){ + throw new IllegalArgumentException("IDs don't match"); + } + Post post = convertToEntity(postDto); postService.updatePost(post); }