JSCODE Logo
프로그래밍 과외블로그후기멘토진
회사명 : JSCODE대표 : 박재성사업자 등록번호 : 244-22-01557통신판매업 : 제 2023-인천미추홀-0381 호
학원 명칭 : 제이에스코드(JSCODE)원격학원학원설립ㆍ운영 등록번호 : 제6063호

서울특별시 구로구 경인로 20가길 11(오류동, 아델리아)

Copyright ⓒ 2025 JSCODE - 최상위 현업 개발자들의 프로그래밍 교육 All rights reserved.

이용약관개인정보처리방침
← 블로그 목록으로 돌아가기

[실습] 게시글 작성 로직 수정하기

JSCODE 박재성
JSCODE 박재성
2025-12-06
author
JSCODE 박재성
category
MSA
createdAt
Dec 6, 2025
series
비전공자도 이해할 수 있는 MSA 입문/실전
slug
practice-modify-post-create-logic
type
post
updatedAt
Dec 6, 2025 05:47 AM

✅ 게시글 작성 로직 수정하기

현재 게시글 작성 API는 Request Body에 포함된 userId 값을 기반으로 로직을 처리하고 있다. 하지만 이 방식은 클라이언트가 임의로 다른 사용자의 userId를 입력해 악의적인 요청을 보낼 수 있다는 보안 문제가 있다. 따라서 API Gateway에서 JWT 토큰을 파싱해 생성한 X-User-Id 헤더 값을 활용하도록 코드를 수정해야 한다.
 
 

✅ 코드 수정하기

board-service에서 코드 수정하기
  1. DTO 수정하기
    1. dto/CreateBoardRequestDto
      public class CreateBoardRequestDto { private String title; private String content; private Long userId; public String getTitle() { return title; } public String getContent() { return content; } public Long getUserId() { return userId; } }
       
  1. Controller 수정하기
    1. controller/BoardController
      @RestController @RequestMapping("/api/boards") public class BoardController { ... @PostMapping public ResponseEntity<Void> create( @RequestBody CreateBoardRequestDto createBoardRequestDto, @RequestHeader("X-User-Id") Long userId ) { boardService.create(createBoardRequestDto, userId); return ResponseEntity.noContent().build(); } }
       
  1. Service 수정하기
    1. service/BoardService
      public void create(CreateBoardRequestDto createBoardRequestDto, Long userId) { // 게시글 저장을 성공했는 지 판단하는 플래그 boolean isBoardCreated = false; Long savedBoardId = null; // 포인트 차감을 성공했는 지 판단하는 플래그 boolean isPointDeducted = false; try { // 게시글 작성 전 100 포인트 차감 pointClient.deductPoints(userId, 100); isPointDeducted = true; // 포인트 차감 성공 플래그 System.out.println("포인트 차감 성공"); // 게시글 작성 Board board = new Board( createBoardRequestDto.getTitle(), createBoardRequestDto.getContent(), userId ); Board savedBoard = this.boardRepository.save(board); savedBoardId = savedBoard.getBoardId(); isBoardCreated = true; // 게시글 저장 성공 플래그 System.out.println("게시글 저장 성공"); // '게시글 작성 완료' 이벤트 발행 BoardCreatedEvent boardCreatedEvent = new BoardCreatedEvent(userId); this.kafkaTemplate.send("board.created", toJsonString(boardCreatedEvent)); } catch (Exception e) { if (isBoardCreated) { // 게시글 작성 보상 트랜잭션 => 게시글 삭제 this.boardRepository.deleteById(savedBoardId); System.out.println("[보상 트랜잭션] 게시글 삭제"); } if (isPointDeducted) { // 포인트 차감 보상 트랜잭션 => 포인트 적립 pointClient.addPoints(userId, 100); System.out.println("[보상 트랜잭션] 포인트 적립"); } // 실패 응답으로 처리하기 위해 예외 던지기 throw e; } }
       
  1. 서버 다시 실행시키기
    1. notion image
       
👨🏻‍🏫
다음 강의에서 ‘게시글 작성 API’ 뿐만 아니라 구현한 모든 API가 정상적으로 잘 작동하는 지 테스트해보자.
author
JSCODE 박재성
category
MSA
createdAt
Dec 6, 2025
series
비전공자도 이해할 수 있는 MSA 입문/실전
slug
type
series-footer
updatedAt
Dec 6, 2025 05:45 AM
📎
이 글은 비전공자도 이해할 수 있는 MSA 입문/실전 강의의 수업 자료 중 일부입니다.