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

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

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

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

[실습] 다른 마이크로서비스가 사용할 ‘사용자 조회 API’ 만들기

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

✅ 게시글 조회 API를 구현하려면, 사용자 조회 API도 만들어야 하는 이유

[게시글 조회 API의 응답값]
{ "boardId": 1, "title": "제목", "content": "내용" "user": { "userId": 1, "name": "박재성" } }
 
[구조]
notion image
게시글 조회 API 응답값에서는 사용자 정보도 필요하기 때문에, 게시글 서비스가 사용할 사용자 정보 조회 API가 필요하다.
 
 

✅ 사용자 조회 API 만들기

user-service 프로젝트에서 아래 코드 작성하기
  1. Response DTO 만들기
    1. dto/UserResponseDto
      public class UserResponseDto { private Long userId; private String email; private String name; public UserResponseDto(Long userId, String email, String name) { this.userId = userId; this.email = email; this.name = name; } public Long getUserId() { return userId; } public String getEmail() { return email; } public String getName() { return name; } }
      • password는 노출되면 안 되는 정보이기 때문에 제외했다.
       
  1. Service 로직 작성하기
    1. service/UserService
      @Service public class UserService { private final UserRepository userRepository; public UserService(UserRepository userRepository) { this.userRepository = userRepository; } ... public UserResponseDto getUser(Long id) { User user = userRepository.findById(id) .orElseThrow(() -> new IllegalArgumentException("사용자를 찾을 수 없습니다.")); return new UserResponseDto( user.getUserId(), user.getEmail(), user.getName() ); } }
       
  1. Controller 로직 작성하기
    1. @RestController @RequestMapping("/users") public class UserController { private final UserService userService; public UserController(UserService userService) { this.userService = userService; } ... @GetMapping("{userId}") public ResponseEntity<UserResponseDto> getUser(@PathVariable Long userId) { UserResponseDto userResponseDto = userService.getUser(userId); return ResponseEntity.ok(userResponseDto); } }
       
  1. 서버 재실행시킨 후 잘 작동하는 지 확인해보기
    1. notion image
 
author
JSCODE 박재성
category
MSA
createdAt
Dec 6, 2025
series
비전공자도 이해할 수 있는 MSA 입문/실전
slug
type
series-footer
updatedAt
Dec 6, 2025 05:45 AM
📎
이 글은 비전공자도 이해할 수 있는 MSA 입문/실전 강의의 수업 자료 중 일부입니다.