From e344af2fbccd54ede10edfc8691f2aedb731a978 Mon Sep 17 00:00:00 2001 From: Dongha Date: Mon, 29 Jan 2024 23:29:40 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Refactor:=20CommonResponse=EA=B0=80=20?= =?UTF-8?q?=EB=8D=94=20=EC=9D=B4=EC=83=81=20=EC=83=81=ED=83=9C=EC=BD=94?= =?UTF-8?q?=EB=93=9C=EB=A5=BC=20=EC=A4=91=EB=B3=B5=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EA=B0=80=EC=A7=80=EC=A7=80=20=EC=95=8A=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20+=20=EC=84=B1=EA=B3=B5=20=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=20=EC=83=81=ED=83=9C=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=A0=90=EA=B2=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CategoryController.java | 2 +- .../controller/ChatMessageController.java | 10 +++--- .../hive/controller/HiveController.java | 33 +++++++------------ .../controller/NotificationController.java | 14 +++----- .../party/controller/PartyController.java | 18 +++++----- .../user/controller/UserController.java | 33 ++++++++----------- .../hhive/global/common/CommonResponse.java | 6 ++-- .../global/jwt/JwtAuthorizationFilter.java | 2 +- 8 files changed, 48 insertions(+), 70 deletions(-) diff --git a/src/main/java/com/HHive/hhive/domain/category/controller/CategoryController.java b/src/main/java/com/HHive/hhive/domain/category/controller/CategoryController.java index d67eae9..5a8e50f 100644 --- a/src/main/java/com/HHive/hhive/domain/category/controller/CategoryController.java +++ b/src/main/java/com/HHive/hhive/domain/category/controller/CategoryController.java @@ -28,7 +28,7 @@ public ResponseEntity>>> getAllCategorie Map> categories = categoryService.getAllCategories(); - return ResponseEntity.ok().body(CommonResponse.of(200, "카테고리 조회 성공", categories)); + return ResponseEntity.ok().body(CommonResponse.of("카테고리 조회 성공", categories)); } } diff --git a/src/main/java/com/HHive/hhive/domain/chatmessage/controller/ChatMessageController.java b/src/main/java/com/HHive/hhive/domain/chatmessage/controller/ChatMessageController.java index 19dc8ee..9336218 100644 --- a/src/main/java/com/HHive/hhive/domain/chatmessage/controller/ChatMessageController.java +++ b/src/main/java/com/HHive/hhive/domain/chatmessage/controller/ChatMessageController.java @@ -34,8 +34,8 @@ public ResponseEntity> sendChatMessage( chatMessageService.sendChatMessages(hiveId, requestDTO, userDetails.getUser()); - return ResponseEntity.ok().body( - CommonResponse.of(HttpStatus.CREATED.value(), "메시지 전송 성공", null)); + return ResponseEntity.status(HttpStatus.CREATED).body( + CommonResponse.of("메시지 전송 성공", null)); } @GetMapping("/hives/{hiveId}") @@ -46,8 +46,7 @@ public ResponseEntity>> getChatMessa List responseDTOList = chatMessageService.getChatMessages(hiveId, userDetails.getUser()); - return ResponseEntity.ok().body( - CommonResponse.of(HttpStatus.OK.value(), "메시지 조회 성공", responseDTOList)); + return ResponseEntity.ok().body(CommonResponse.of("메시지 조회 성공", responseDTOList)); } @DeleteMapping("/{chat-messagesId}") @@ -57,7 +56,6 @@ public ResponseEntity> deleteChatMessage( chatMessageService.deleteChatMessage(chatMessageId, userDetails.getUser()); - return ResponseEntity.ok().body( - CommonResponse.of(HttpStatus.NO_CONTENT.value(), "메시지 삭제 성공", null)); + return ResponseEntity.ok().body(CommonResponse.of("메시지 삭제 성공", null)); } } diff --git a/src/main/java/com/HHive/hhive/domain/hive/controller/HiveController.java b/src/main/java/com/HHive/hhive/domain/hive/controller/HiveController.java index 38dfddf..2fa2f03 100644 --- a/src/main/java/com/HHive/hhive/domain/hive/controller/HiveController.java +++ b/src/main/java/com/HHive/hhive/domain/hive/controller/HiveController.java @@ -44,8 +44,8 @@ public ResponseEntity> createHive( SubCategory.findByStringName(createHiveRequestDTO.getSubCategoryName()); HiveResponseDTO response = hiveService.createHive(userDetails.getUser(), createHiveRequestDTO); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "하이브가 작성되었습니다.", response)); + return ResponseEntity.status(HttpStatus.CREATED) + .body(CommonResponse.of("하이브가 작성되었습니다.", response)); } @PatchMapping("{hive_id}/title") @@ -54,8 +54,7 @@ public ResponseEntity> updateHiveTitle( @RequestBody @Valid UpdateHiveTitleRequestDTO updateHiveTitleRequest) { HiveResponseDTO response = hiveService.updateHiveTitle(userDetails.getUser(), hive_id, updateHiveTitleRequest); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "하이브가 수정되었습니다.", response)); + return ResponseEntity.ok().body(CommonResponse.of("하이브가 수정되었습니다.", response)); } @PatchMapping("{hive_id}/info") @@ -64,8 +63,7 @@ public ResponseEntity> updateHiveInfo( @RequestBody @Valid UpdateHiveInfoRequestDTO updateHiveInfoRequest) { HiveResponseDTO response = hiveService.updateHiveInfo(userDetails.getUser(), hive_id, updateHiveInfoRequest); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "하이브가 수정되었습니다.", response)); + return ResponseEntity.ok().body(CommonResponse.of("하이브가 수정되었습니다.", response)); } @GetMapping("/{hive_id}") @@ -73,15 +71,13 @@ public ResponseEntity> getHive( @AuthenticationPrincipal UserDetailsImpl userDetails, @PathVariable Long hive_id) { HiveResponseDTO response = hiveService.getHive(userDetails.getUser(), hive_id); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "하이브가 조회되었습니다.", response)); + return ResponseEntity.ok().body(CommonResponse.of("하이브가 조회되었습니다.", response)); } @GetMapping("") public ResponseEntity>> gethives() { List responses = hiveService.getHives(); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "하이브들이 조회되었습니다.", responses)); + return ResponseEntity.ok().body(CommonResponse.of("하이브들이 조회되었습니다.", responses)); } @GetMapping("/search") @@ -91,8 +87,7 @@ public ResponseEntity>> getHivesByCategory( List responses = hiveService.getHivesByCategory( MajorCategory.findByStringName(majorCategory), SubCategory.findByStringName(subCategory)); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "하이브들이 조회되었습니다.", responses)); + return ResponseEntity.ok().body(CommonResponse.of("하이브들이 조회되었습니다.", responses)); } @@ -101,8 +96,7 @@ public ResponseEntity> deleteHive( @AuthenticationPrincipal UserDetailsImpl userDetails, @PathVariable Long hive_id) { hiveService.deleteHive(userDetails.getUser(), hive_id); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "하이브가 삭제되었습니다.", null)); + return ResponseEntity.ok().body(CommonResponse.of("하이브가 삭제되었습니다.", null)); } @PostMapping("/{hive_id}") @@ -111,8 +105,7 @@ public ResponseEntity> inviteNewUser( @PathVariable Long hive_id, @RequestBody @Valid HiveUserInviteRequestDTO requestDTO) { hiveService.inviteNewUser(userDetails.getUser(), hive_id, requestDTO); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "하이브에 참여하였습니다.", null)); + return ResponseEntity.ok().body(CommonResponse.of("하이브에 참여하였습니다.", null)); } @GetMapping("/{hive_id}/hiveUsers") @@ -122,8 +115,7 @@ public ResponseEntity>> getHiveUsersInH List hiveUserResponses = hiveService.searchUsersInHive( userDetails.getUser(), hive_id); return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "하이브 유저들이 조회되었습니다.", - hiveUserResponses)); + .body(CommonResponse.of("하이브 유저들이 조회되었습니다.", hiveUserResponses)); } @GetMapping("/{hive_id}/hiveUsers/search") @@ -134,8 +126,7 @@ public ResponseEntity> getHiveUserInHive( UserInfoResponseDTO hiveUserResponse = hiveService.searchUserInHive(userDetails.getUser(), hive_id, username); return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "하이브 유저가 조회되었습니다.", - hiveUserResponse)); + .body(CommonResponse.of("하이브 유저가 조회되었습니다.", hiveUserResponse)); } @DeleteMapping("{hive_id}/hiveUsers") @@ -145,7 +136,7 @@ public ResponseEntity> deleteHiveUser( @RequestParam String username) { hiveService.deleteHiveUser(userDetails.getUser(), hive_id, username); return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "하이브 유저가 탈퇴되었습니다.", null)); + .body(CommonResponse.of("하이브 유저가 탈퇴되었습니다.", null)); } } \ No newline at end of file diff --git a/src/main/java/com/HHive/hhive/domain/notification/controller/NotificationController.java b/src/main/java/com/HHive/hhive/domain/notification/controller/NotificationController.java index fa7a373..9a8d9b6 100644 --- a/src/main/java/com/HHive/hhive/domain/notification/controller/NotificationController.java +++ b/src/main/java/com/HHive/hhive/domain/notification/controller/NotificationController.java @@ -38,7 +38,7 @@ public ResponseEntity sendNotification( notificationRequestDTO); return ResponseEntity.status(HttpStatus.CREATED) - .body(CommonResponse.of(HttpStatus.CREATED.value(), "메시지 전송 성공", response)); + .body(CommonResponse.of("메시지 전송 성공", response)); } @GetMapping(value = "/{userId}/get" ) @@ -58,8 +58,7 @@ public ResponseEntity getNotificationsByUserId( ) { List notifications = notificationService.getNotificationsByUserId( userDetails.getUser().getId()); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "알림 출력 성공", notifications)); + return ResponseEntity.ok().body(CommonResponse.of("알림 출력 성공", notifications)); } @GetMapping("/count") @@ -69,8 +68,7 @@ public ResponseEntity showUnreadNotificationCountForUser( Long notificationCount = notificationService.showUnreadNotificationCountForUser( userDetails.getUser() .getId()); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "알림 출력 성공", notificationCount)); + return ResponseEntity.ok().body(CommonResponse.of("알림 출력 성공", notificationCount)); } @DeleteMapping("/{notificationId}") @@ -78,16 +76,14 @@ public ResponseEntity deleteNotification( @PathVariable(name = "notificationId") Long notificationId ) { notificationService.deleteNotification(notificationId); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "알림 개수 조회 완료", null)); + return ResponseEntity.ok().body(CommonResponse.of("알림 개수 조회 완료", null)); } @GetMapping("/read") public ResponseEntity readNotification( @AuthenticationPrincipal UserDetailsImpl userDetails ){ notificationService.readNotification(userDetails.getUser().getId()); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "알림 열람 완료", null)); + return ResponseEntity.ok().body(CommonResponse.of("알림 열람 완료", null)); } @DeleteMapping("/clear") public void clear(){ diff --git a/src/main/java/com/HHive/hhive/domain/party/controller/PartyController.java b/src/main/java/com/HHive/hhive/domain/party/controller/PartyController.java index 9da978e..f30c77b 100644 --- a/src/main/java/com/HHive/hhive/domain/party/controller/PartyController.java +++ b/src/main/java/com/HHive/hhive/domain/party/controller/PartyController.java @@ -29,7 +29,8 @@ public ResponseEntity> createParty( @AuthenticationPrincipal UserDetailsImpl userDetails) { PartyResponseDTO responseDto = partyService.createParty(hiveId, partyRequestDto, userDetails.getUser()); - return ResponseEntity.ok().body(CommonResponse.of(HttpStatus.OK.value(), "파티 생성 성공", responseDto)); + return ResponseEntity.status(HttpStatus.CREATED) + .body(CommonResponse.of("파티 생성 성공", responseDto)); } @GetMapping("/{partyId}/hives/{hiveId}") @@ -37,7 +38,7 @@ public ResponseEntity> getParty(@PathVariable L PartyResponseDTO responseDto = partyService.getPartyDto(hiveId, partyId); - return ResponseEntity.ok().body(CommonResponse.of(HttpStatus.OK.value(), "파티 조회 성공", responseDto)); + return ResponseEntity.ok().body(CommonResponse.of("파티 조회 성공", responseDto)); } @@ -48,7 +49,7 @@ public ResponseEntity>> getPartyList(@ .map(entry -> new PartyListResponseDTO(entry.getKey(), entry.getValue())) .collect(Collectors.toList()); - return ResponseEntity.ok().body(CommonResponse.of(HttpStatus.OK.value(), "파티 목록 조회 성공", response)); + return ResponseEntity.ok().body(CommonResponse.of("파티 목록 조회 성공", response)); } @PatchMapping("/{partyId}") @@ -58,7 +59,7 @@ public ResponseEntity> updateParty(@PathVariabl PartyResponseDTO responseDto = partyService.updateParty(partyId, partyRequestDto, userDetails.getUser()); - return ResponseEntity.ok().body(CommonResponse.of(HttpStatus.OK.value(), "업데이트 성공", responseDto)); + return ResponseEntity.ok().body(CommonResponse.of("업데이트 성공", responseDto)); } @DeleteMapping("/{partyId}") @@ -67,7 +68,7 @@ public ResponseEntity> deleteParty(@PathVariable Long par partyService.deleteParty(partyId, userDetails.getUser()); - return ResponseEntity.ok().body(CommonResponse.of(HttpStatus.OK.value(), "정상적으로 삭제 되었습니다.", null)); + return ResponseEntity.ok().body(CommonResponse.of("정상적으로 삭제 되었습니다.", null)); } @PostMapping("/{partyId}/join") @@ -76,7 +77,7 @@ public ResponseEntity> joinParty(@PathVariable Long party partyService.joinParty(partyId, userDetails.getUser()); - return ResponseEntity.ok().body(CommonResponse.of(HttpStatus.OK.value(), "파티 가입 성공", null)); + return ResponseEntity.ok().body(CommonResponse.of("파티 가입 성공", null)); } @DeleteMapping("/{partyId}/resign") @@ -85,7 +86,7 @@ public ResponseEntity> resignParty(@PathVariable Long par partyService.resignParty(partyId, userDetails.getUser()); - return ResponseEntity.ok().body(CommonResponse.of(HttpStatus.OK.value(), "파티 탈퇴 성공", null)); + return ResponseEntity.ok().body(CommonResponse.of("파티 탈퇴 성공", null)); } @GetMapping("/users/{userId}") @@ -94,8 +95,7 @@ public ResponseEntity>> getPartiesByUserId List response = partyService.getPartiesByUserId(userId); - return ResponseEntity.ok().body(CommonResponse.of(HttpStatus.OK.value(), - "유저의 파티 조회 성공", response)); + return ResponseEntity.ok().body(CommonResponse.of("유저의 파티 조회 성공", response)); } } diff --git a/src/main/java/com/HHive/hhive/domain/user/controller/UserController.java b/src/main/java/com/HHive/hhive/domain/user/controller/UserController.java index 6940047..6d29a77 100644 --- a/src/main/java/com/HHive/hhive/domain/user/controller/UserController.java +++ b/src/main/java/com/HHive/hhive/domain/user/controller/UserController.java @@ -35,7 +35,8 @@ public ResponseEntity> signup( @Valid @RequestBody UserSignupRequestDTO requestDTO) { userService.signup(requestDTO); - return ResponseEntity.ok().body(CommonResponse.of(HttpStatus.CREATED.value(), "회원가입 성공", null)); + return ResponseEntity.status(HttpStatus.CREATED) + .body(CommonResponse.of("회원가입 성공", null)); } // 이메일 인증 코드 생성 @@ -49,7 +50,8 @@ public ResponseEntity> mailConfirm(@PathVariable Long us // 생성된 이메일 인증 코드를 사용자 엔티티에 저장 userService.requestEmailVerification(userId, verificationCode); - return ResponseEntity.ok().body(CommonResponse.of(HttpStatus.CREATED.value(), "인증코드 발급 성공", verificationCode)); + return ResponseEntity.status(HttpStatus.CREATED) + .body(CommonResponse.of("인증코드 발급 성공", verificationCode)); } // 이메일 인증 코드 검증 @@ -58,7 +60,7 @@ public ResponseEntity> verifyEmail(@PathVariable Long userI userService.verifyEmail(userId, requestDTO.getVerificationCode()); - return ResponseEntity.ok().body(CommonResponse.of(HttpStatus.OK.value(), "이메일 인증 성공", null)); + return ResponseEntity.ok().body(CommonResponse.of("이메일 인증 성공", null)); } @@ -71,16 +73,14 @@ public ResponseEntity> login( response.setHeader(JwtUtil.AUTHORIZATION_HEADER, jwtUtil.createToken(requestDTO.getUsername())); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "로그인 성공", userInfo)); + return ResponseEntity.ok().body(CommonResponse.of("로그인 성공", userInfo)); } @GetMapping("/{userId}") public ResponseEntity> getProfile(@PathVariable Long userId) { UserInfoResponseDTO responseDTO = userService.getProfile(userId); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "프로필 조회 성공", responseDTO)); + return ResponseEntity.ok().body(CommonResponse.of("프로필 조회 성공", responseDTO)); } @@ -94,8 +94,7 @@ public ResponseEntity> updateProfile( User loginUser = userDetails.getUser(); userService.updateProfile(userId, requestDTO, loginUser); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "프로필 수정 성공", null)); + return ResponseEntity.ok().body(CommonResponse.of("프로필 수정 성공", null)); } @PatchMapping("/{userId}/password") @@ -107,8 +106,7 @@ public ResponseEntity> updatePassword( User loginUser = userDetails.getUser(); userService.updatePassword(userId, requestDTO, loginUser); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "비밀번호 수정 성공", null)); + return ResponseEntity.ok().body(CommonResponse.of("비밀번호 수정 성공", null)); } @DeleteMapping("/{userId}") @@ -119,18 +117,17 @@ public ResponseEntity> deleteUser( User loginUser = userDetails.getUser(); userService.deleteUser(userId, loginUser); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "회원 탈퇴 성공", null)); + return ResponseEntity.ok().body(CommonResponse.of("회원 탈퇴 성공", null)); } @GetMapping("/{userId}/hives") - public ResponseEntity getMyHives( + public ResponseEntity>> getMyHives( @PathVariable Long userId) { List hivesResponses = userService.getMyHives( userId); return ResponseEntity.ok() - .body(new CommonResponse<>(200, "참여 목록들이 조회되었습니다.", hivesResponses)); + .body(CommonResponse.of("참여 목록들이 조회되었습니다.", hivesResponses)); } @@ -143,8 +140,7 @@ public ResponseEntity> setCategory( User loginUser = userDetails.getUser(); UserCategoryResponseDTO response = userService.setCategory(userId, requestDTO, loginUser); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.OK.value(), "카테고리 설정 성공", response)); + return ResponseEntity.ok().body(CommonResponse.of("카테고리 설정 성공", response)); } @GetMapping("/kakao/callback") @@ -161,7 +157,6 @@ public ResponseEntity> kakaoLogin(@RequestPa // username으로 UserInfoResponseDTO를 얻음 UserInfoResponseDTO userInfo = userService.kakaoLogin(username); - return ResponseEntity.ok() - .body(CommonResponse.of(HttpStatus.CREATED.value(), "카카오 로그인 성공", userInfo)); + return ResponseEntity.ok().body(CommonResponse.of("카카오 로그인 성공", userInfo)); } } diff --git a/src/main/java/com/HHive/hhive/global/common/CommonResponse.java b/src/main/java/com/HHive/hhive/global/common/CommonResponse.java index 8ff8a7b..dcc8b26 100644 --- a/src/main/java/com/HHive/hhive/global/common/CommonResponse.java +++ b/src/main/java/com/HHive/hhive/global/common/CommonResponse.java @@ -7,13 +7,11 @@ @RequiredArgsConstructor public class CommonResponse { - private final int statusCode; - private final String message; private final T payload; - public static CommonResponse of(int statusCode, String message, T payload) { - return new CommonResponse<>(statusCode, message, payload); + public static CommonResponse of(String message, T payload) { + return new CommonResponse<>(message, payload); } } diff --git a/src/main/java/com/HHive/hhive/global/jwt/JwtAuthorizationFilter.java b/src/main/java/com/HHive/hhive/global/jwt/JwtAuthorizationFilter.java index 08d8f23..442b37f 100644 --- a/src/main/java/com/HHive/hhive/global/jwt/JwtAuthorizationFilter.java +++ b/src/main/java/com/HHive/hhive/global/jwt/JwtAuthorizationFilter.java @@ -59,7 +59,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse SecurityContextHolder.setContext(context); } else { // 유효하지 않은 인증 토큰일 경우 CommonResponse commonResponse = new CommonResponse( - 401, "토큰이 유효하지 않습니다.", HttpStatus.BAD_REQUEST.value()); + "토큰이 유효하지 않습니다.", HttpStatus.BAD_REQUEST.value()); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.setContentType("application/json; charset=UTF-8"); response.getWriter().write(objectMapper.writeValueAsString(commonResponse)); From 4da135ebe3df402f8187ece3f83e969942f2b949 Mon Sep 17 00:00:00 2001 From: Dongha Date: Mon, 29 Jan 2024 23:47:12 +0900 Subject: [PATCH 2/2] =?UTF-8?q?Refactor:=20jakarata.validation=20=EC=97=90?= =?UTF-8?q?=EC=84=9C=EC=9D=98=20=EC=97=90=EB=9F=AC=EB=A9=94=EC=8B=9C?= =?UTF-8?q?=EC=A7=80=20=ED=94=84=EB=A1=A0=ED=8A=B8=EC=97=90=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/user/dto/UserSignupRequestDTO.java | 4 ++-- .../common/GlobalExceptionHandler.java | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/HHive/hhive/domain/user/dto/UserSignupRequestDTO.java b/src/main/java/com/HHive/hhive/domain/user/dto/UserSignupRequestDTO.java index 6d69833..ed52343 100644 --- a/src/main/java/com/HHive/hhive/domain/user/dto/UserSignupRequestDTO.java +++ b/src/main/java/com/HHive/hhive/domain/user/dto/UserSignupRequestDTO.java @@ -7,11 +7,11 @@ public class UserSignupRequestDTO { @Pattern(regexp = "^[a-zA-Z0-9]{4,10}$", - message = "올바른 형식이 아닙니다. 문자(대문자/소문자) 혹은 숫자를 4글자 이상 10글자 이하로 작성해주세요.") + message = "올바른 형식의 아이디가 아닙니다. 문자(대문자/소문자) 혹은 숫자를 4글자 이상 10글자 이하로 작성해주세요.") private String username; @Pattern(regexp = "^[a-zA-Z0-9]{4,}$", - message = "올바른 형식이 아닙니다. 문자(대문자/소문자) 혹은 숫자를 4글자 이상 작성해주세요.") + message = "올바른 형식의 비밀번호가 아닙니다. 문자(대문자/소문자) 혹은 숫자를 4글자 이상 작성해주세요.") private String password; private String checkPassword; diff --git a/src/main/java/com/HHive/hhive/global/exception/common/GlobalExceptionHandler.java b/src/main/java/com/HHive/hhive/global/exception/common/GlobalExceptionHandler.java index ed4a3c6..877530d 100644 --- a/src/main/java/com/HHive/hhive/global/exception/common/GlobalExceptionHandler.java +++ b/src/main/java/com/HHive/hhive/global/exception/common/GlobalExceptionHandler.java @@ -1,8 +1,11 @@ package com.HHive.hhive.global.exception.common; +import jakarta.validation.ValidationException; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; @@ -20,4 +23,18 @@ protected ResponseEntity handleCustomException(CustomException e) return ResponseEntity.status(httpStatus).body(errorResponse); } + + @ExceptionHandler(MethodArgumentNotValidException.class) + protected ResponseEntity handleValidationException(MethodArgumentNotValidException ex) { + BindingResult bindingResult = ex.getBindingResult(); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(getValidationMessages(bindingResult)); + } + + private String getValidationMessages(BindingResult bindingResult) { + return bindingResult.getAllErrors() + .stream() + .findFirst() + .map(error -> error.getDefaultMessage()) + .orElse("Validation failed"); + } }