프레임워크/Spring

[Spring Boot] Spring CRUD 구현 - Day 1

munsik22 2026. 3. 10. 19:09

프로젝트 목표

  • 게시판 서버 API 구현
    • 회원가입/로그인
    • 게시물 작성
    • 게시물 목록보기
    • 게시물 읽기
    • 댓글 작성

코드 구현

  • Post.java: 게시판 엔티티 클래스
package com.example.springcrudback.post;

import lombok.Getter;
import lombok.Setter;

@Getter @Setter
public class Post {

    private Long id;
    private String title;
    private String content;

    public Post() {
    }

    public Post(Long id, String title, String content) {
        this.id = id;
        this.title = title;
        this.content = content;
    }

}
  • PostRequest.java: 요청 바디용 클래스
package com.example.springcrudback.post;

import lombok.Getter;
import lombok.Setter;

@Getter @Setter
public class PostRequest {

    private String title;
    private String content;

    public PostRequest() {
    }

    public PostRequest(String title, String content) {
        this.title = title;
        this.content = content;
    }

}
  • PostController.java: 컨트롤러
package com.example.springcrudback.post;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.concurrent.atomic.AtomicLong;

@RestController @RequestMapping("/posts")
public class PostController {
    private final Map<Long, Post> postsStore = new LinkedHashMap<>();
    private final AtomicLong sequence = new AtomicLong(1);

    @PostMapping @ResponseStatus(HttpStatus.CREATED)
    public Post createPost(@RequestBody PostRequest request) {
        Long id = sequence.getAndIncrement();
        Post post = new Post(id, request.getTitle(), request.getContent());
        postsStore.put(id, post);
        return post;
    }

    @GetMapping @ResponseStatus(HttpStatus.OK)
    public List<Post> getPosts() {
        return new ArrayList<>(postsStore.values());
    }

    @GetMapping("/{id}")
    public Post getPost(@PathVariable Long id) {
        Post post = postsStore.get(id);
        if (post == null) {
            throw new IllegalArgumentException("Post with id " + id + " does not exist");
        }
        return post;
    }

    @PutMapping("/{id}")
    public Post updatePost(@PathVariable Long id, @RequestBody PostRequest postRequest) {
        Post post = postsStore.get(id);
        if (post == null) {
            throw new IllegalArgumentException("Post with id " + id + " does not exist");
        }
        post.setTitle(postRequest.getTitle());
        post.setContent(postRequest.getContent());
        return post;
    }

    @DeleteMapping("/{id}") @ResponseStatus(HttpStatus.NO_CONTENT)
    public void deletePost(@PathVariable Long id) {
        Post post = postsStore.get(id);
        if (post == null) {
            throw new IllegalArgumentException("Post with id " + id + " does not exist");
        }
        postsStore.remove(id);
    }
}

코드 설명

  • @RestController: 이 클래스가 REST API용 컨트롤러라는 뜻이다.
  • @RequestMapping("/posts"): 이 컨트롤러의 기본 주소를 /posts로 잡는다.
  • @PostMapping: 새 데이터를 만든다.
  • @GetMapping: 데이터를 조회한다.
  • @PutMapping: 기존 데이터를 수정한다.
  • @DeleteMapping: 기존 데이터를 삭제한다.
  • @RequestBody: 요청 본문(JSON)을 자바 객체로 바꿔서 받는다.
  • @PathVariable: URL 경로의 값을 꺼냅니다. (예: /posts/1 에서 1을 꺼냄)

테스트

Postman Desktop App을 사용해서 API 테스트를 진행했다.

  • 게시글 생성

  • 게시글 전체 조회

  • 게시글 단건 조회

  • 게시글 수정

  • 게시글 삭제

성공하면 204 No Content가 나옴


오늘 한 일

  • 클라이언트가 /posts 로 요청
  • Spring MVC가 컨트롤러로 보냄
  • 컨트롤러가 HashMap에 데이터 저장/조회/수정/삭제
  • 결과를 JSON으로 응답

내일 할 일

 

  • Service 추가
  • Repository 추가
  • H2 DB 연결
  • JPA 사용

 

'프레임워크 > Spring' 카테고리의 다른 글

[Spring Boot] Spring CRUD 구현 - Day 2  (0) 2026.03.11
[Spring] JPA와 H2 DB  (0) 2026.03.10
[Spring] Spring MVC  (0) 2026.03.09
[Spring Boot] 오목 게임 만들기 - Day 2  (0) 2026.02.23
[Spring Boot] 오목 게임 만들기 - Day 1  (0) 2026.02.21