이번 글에서는 Spring Boot REST API 실습 20강 내용을 정리한다.
그동안 구현해온 API를 Postman으로 직접 호출하며 검증해보는 단계다.
이번 강의의 핵심은
“프론트가 없어도 REST API는 충분히 검증할 수 있다”
라는 점을 체감하는 것이다.
REST API 방식에서는
서버는 JSON 데이터만 책임지고,
화면과 UI는 프론트엔드의 몫이다.
GET http://localhost:8080/api/v1/posts
@GetMapping
@Transactional(readOnly = true)
public List<PostDto> getItems() {
List<Post> items = postService.findAll();
return items
.stream()
.map(PostDto::new)
.toList();
}

GET http://localhost:8080/api/v1/posts/2
@GetMapping("/{id}")
@Transactional(readOnly = true)
public PostDto getItem(@PathVariable int id) {
Post post = postService.findById(id).get();
return new PostDto(post);
}

GET http://localhost:8080/api/v1/posts/2/comments
@GetMapping
@Transactional(readOnly = true)
public List<PostCommentDto> getItems(
@PathVariable int postId
) {
Post post = postService.findById(postId).get();
return post.getComments()
.stream()
.map(PostCommentDto::new)
.toList();
}

GET http://localhost:8080/api/v1/posts/2/comments/4
@GetMapping("/{id}")
@Transactional(readOnly = true)
public PostCommentDto getItem(
@PathVariable int postId,
@PathVariable int id
) {
Post post = postService.findById(postId).get();
PostComment postComment = post.findCommentById(id).get();
return new PostCommentDto(postComment);
}

REST API 개발에서
Postman은 거의 필수 도구라고 봐도 무방하다.
프론트가 없어도 REST API는
Postman만으로 충분히 검증할 수 있다.