Skip to content

Commit 3958dcf

Browse files
authored
BAEL-5313: Use id path variable in the PUT mapping (eugenp#11634)
1 parent 80edc7f commit 3958dcf

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

spring-boot-rest/src/main/java/com/baeldung/springpagination/controller/PostRestController.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.text.ParseException;
2121
import java.util.List;
22+
import java.util.Objects;
2223
import java.util.stream.Collectors;
2324

2425
@Controller
@@ -65,7 +66,11 @@ public PostDto getPost(@PathVariable("id") Long id) {
6566

6667
@PutMapping(value = "/{id}")
6768
@ResponseStatus(HttpStatus.OK)
68-
public void updatePost(@RequestBody PostDto postDto) throws ParseException {
69+
public void updatePost(@PathVariable("id") Long id, @RequestBody PostDto postDto) throws ParseException {
70+
if(!Objects.equals(id, postDto.getId())){
71+
throw new IllegalArgumentException("IDs don't match");
72+
}
73+
6974
Post post = convertToEntity(postDto);
7075
postService.updatePost(post);
7176
}

0 commit comments

Comments
 (0)