![](https://velog.velcdn.com/images%2Fdrv98%2Fpost%2F258511a4-ec7b-40f4-97e0-60ea5363cf68%2Fimage.png)
1. PostService
//ID로 포스트 업데이트
PostDto updatePost(PostDto postDto, Long id);
2. PostServiceImpl
@Override
public PostDto updatePost(PostDto postDto, Long id) {
// id 로 포스트 찾기 없으면 예외 발생(커스텀 예외 생성)
Post post = postRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Post", "id", id));
post.setTitle(postDto.getTitle());
post.setDescription(postDto.getDescription());
post.setContent(postDto.getContent());
Post updatePost = postRepository.save(?);
return mapToDto(?);
}
3. 컨트롤러
@PutMapping("/{id}")
public ResponseEntity<PostDto> updatePost(@RequestBody PostDto postDto, @PathVariable Long id) {
return new ResponseEntity<>(postService.updatePost(postDto, id), HttpStatus.OK);
}
![](https://velog.velcdn.com/images%2Fdrv98%2Fpost%2Fc9293d03-47d2-4428-af8c-5818310e92e8%2Fimage.png)
![](https://velog.velcdn.com/images%2Fdrv98%2Fpost%2F09d2126f-8c8d-4ccb-8cb1-6fe955e3774b%2Fimage.png)