diff --git a/build.gradle b/build.gradle index de5a48bd..c1e84cde 100644 --- a/build.gradle +++ b/build.gradle @@ -24,6 +24,7 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-validation' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.mysql:mysql-connector-j' annotationProcessor 'org.projectlombok:lombok' diff --git a/src/main/java/com/umc/networkingService/domain/album/entity/Album.java b/src/main/java/com/umc/networkingService/domain/album/entity/Album.java index 8cbaf137..859885ea 100644 --- a/src/main/java/com/umc/networkingService/domain/album/entity/Album.java +++ b/src/main/java/com/umc/networkingService/domain/album/entity/Album.java @@ -1,7 +1,7 @@ package com.umc.networkingService.domain.album.entity; import com.umc.networkingService.domain.member.entity.Member; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import com.umc.networkingService.global.common.Semester; import jakarta.persistence.*; import lombok.AccessLevel; diff --git a/src/main/java/com/umc/networkingService/domain/album/entity/AlbumComment.java b/src/main/java/com/umc/networkingService/domain/album/entity/AlbumComment.java index 71c8f5cf..b4e1b15d 100644 --- a/src/main/java/com/umc/networkingService/domain/album/entity/AlbumComment.java +++ b/src/main/java/com/umc/networkingService/domain/album/entity/AlbumComment.java @@ -1,7 +1,7 @@ package com.umc.networkingService.domain.album.entity; import com.umc.networkingService.domain.member.entity.Member; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/com/umc/networkingService/domain/album/entity/AlbumHeart.java b/src/main/java/com/umc/networkingService/domain/album/entity/AlbumHeart.java index f50c2427..e9cc8c3b 100644 --- a/src/main/java/com/umc/networkingService/domain/album/entity/AlbumHeart.java +++ b/src/main/java/com/umc/networkingService/domain/album/entity/AlbumHeart.java @@ -1,7 +1,7 @@ package com.umc.networkingService.domain.album.entity; import com.umc.networkingService.domain.member.entity.Member; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/com/umc/networkingService/domain/album/entity/AlbumImage.java b/src/main/java/com/umc/networkingService/domain/album/entity/AlbumImage.java index 7310e08b..574b2ff1 100644 --- a/src/main/java/com/umc/networkingService/domain/album/entity/AlbumImage.java +++ b/src/main/java/com/umc/networkingService/domain/album/entity/AlbumImage.java @@ -1,6 +1,6 @@ package com.umc.networkingService.domain.album.entity; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/com/umc/networkingService/domain/board/entity/Board.java b/src/main/java/com/umc/networkingService/domain/board/entity/Board.java index 8564d017..ad75259f 100644 --- a/src/main/java/com/umc/networkingService/domain/board/entity/Board.java +++ b/src/main/java/com/umc/networkingService/domain/board/entity/Board.java @@ -1,7 +1,7 @@ package com.umc.networkingService.domain.board.entity; import com.umc.networkingService.domain.member.entity.Member; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import com.umc.networkingService.global.common.Semester; import jakarta.persistence.*; import lombok.AccessLevel; diff --git a/src/main/java/com/umc/networkingService/domain/board/entity/BoardCheck.java b/src/main/java/com/umc/networkingService/domain/board/entity/BoardCheck.java index 79356397..cfdfc12b 100644 --- a/src/main/java/com/umc/networkingService/domain/board/entity/BoardCheck.java +++ b/src/main/java/com/umc/networkingService/domain/board/entity/BoardCheck.java @@ -1,7 +1,7 @@ package com.umc.networkingService.domain.board.entity; import com.umc.networkingService.domain.member.entity.Member; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/com/umc/networkingService/domain/board/entity/BoardComment.java b/src/main/java/com/umc/networkingService/domain/board/entity/BoardComment.java index 99fc9bdc..426d797c 100644 --- a/src/main/java/com/umc/networkingService/domain/board/entity/BoardComment.java +++ b/src/main/java/com/umc/networkingService/domain/board/entity/BoardComment.java @@ -2,7 +2,7 @@ import com.umc.networkingService.domain.member.entity.Member; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/com/umc/networkingService/domain/board/entity/BoardHeart.java b/src/main/java/com/umc/networkingService/domain/board/entity/BoardHeart.java index 668979ba..5ab927bf 100644 --- a/src/main/java/com/umc/networkingService/domain/board/entity/BoardHeart.java +++ b/src/main/java/com/umc/networkingService/domain/board/entity/BoardHeart.java @@ -2,7 +2,7 @@ import com.umc.networkingService.domain.member.entity.Member; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/com/umc/networkingService/domain/board/entity/BoardImage.java b/src/main/java/com/umc/networkingService/domain/board/entity/BoardImage.java index 54edb292..b0f8e454 100644 --- a/src/main/java/com/umc/networkingService/domain/board/entity/BoardImage.java +++ b/src/main/java/com/umc/networkingService/domain/board/entity/BoardImage.java @@ -1,6 +1,6 @@ package com.umc.networkingService.domain.board.entity; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/com/umc/networkingService/domain/branch/entity/Branch.java b/src/main/java/com/umc/networkingService/domain/branch/entity/Branch.java index 4adbdea9..e4f55dd9 100644 --- a/src/main/java/com/umc/networkingService/domain/branch/entity/Branch.java +++ b/src/main/java/com/umc/networkingService/domain/branch/entity/Branch.java @@ -1,6 +1,6 @@ package com.umc.networkingService.domain.branch.entity; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import com.umc.networkingService.global.common.Semester; import jakarta.persistence.*; import lombok.AccessLevel; diff --git a/src/main/java/com/umc/networkingService/domain/branch/entity/BranchUniversity.java b/src/main/java/com/umc/networkingService/domain/branch/entity/BranchUniversity.java index f4347220..cc0d95bc 100644 --- a/src/main/java/com/umc/networkingService/domain/branch/entity/BranchUniversity.java +++ b/src/main/java/com/umc/networkingService/domain/branch/entity/BranchUniversity.java @@ -1,7 +1,7 @@ package com.umc.networkingService.domain.branch.entity; import com.umc.networkingService.domain.university.entity.University; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/com/umc/networkingService/domain/friend/entity/Friend.java b/src/main/java/com/umc/networkingService/domain/friend/entity/Friend.java index d113f6f4..c10078f9 100644 --- a/src/main/java/com/umc/networkingService/domain/friend/entity/Friend.java +++ b/src/main/java/com/umc/networkingService/domain/friend/entity/Friend.java @@ -1,8 +1,9 @@ package com.umc.networkingService.domain.friend.entity; import com.umc.networkingService.domain.member.entity.Member; -import com.umc.networkingService.global.common.BaseEntity; import jakarta.persistence.*; +import com.umc.networkingService.global.common.base.BaseEntity; + import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; diff --git a/src/main/java/com/umc/networkingService/domain/inviteCode/entity/InviteCode.java b/src/main/java/com/umc/networkingService/domain/inviteCode/entity/InviteCode.java index bb779119..e5bbf377 100644 --- a/src/main/java/com/umc/networkingService/domain/inviteCode/entity/InviteCode.java +++ b/src/main/java/com/umc/networkingService/domain/inviteCode/entity/InviteCode.java @@ -1,7 +1,7 @@ package com.umc.networkingService.domain.inviteCode.entity; import com.umc.networkingService.domain.member.entity.Member; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import com.umc.networkingService.global.common.Role; import jakarta.persistence.*; import lombok.AccessLevel; diff --git a/src/main/java/com/umc/networkingService/domain/mascot/entity/Mascot.java b/src/main/java/com/umc/networkingService/domain/mascot/entity/Mascot.java index 2d34addc..cbcdca79 100644 --- a/src/main/java/com/umc/networkingService/domain/mascot/entity/Mascot.java +++ b/src/main/java/com/umc/networkingService/domain/mascot/entity/Mascot.java @@ -1,6 +1,6 @@ package com.umc.networkingService.domain.mascot.entity; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.Id; diff --git a/src/main/java/com/umc/networkingService/domain/member/entity/Member.java b/src/main/java/com/umc/networkingService/domain/member/entity/Member.java index c81132aa..f102c447 100644 --- a/src/main/java/com/umc/networkingService/domain/member/entity/Member.java +++ b/src/main/java/com/umc/networkingService/domain/member/entity/Member.java @@ -2,7 +2,7 @@ import com.umc.networkingService.domain.branch.entity.Branch; import com.umc.networkingService.domain.university.entity.University; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import com.umc.networkingService.global.common.Part; import com.umc.networkingService.global.common.Role; import com.umc.networkingService.global.common.Semester; diff --git a/src/main/java/com/umc/networkingService/domain/member/entity/MemberPoint.java b/src/main/java/com/umc/networkingService/domain/member/entity/MemberPoint.java index f6ed6e62..1b3e3f75 100644 --- a/src/main/java/com/umc/networkingService/domain/member/entity/MemberPoint.java +++ b/src/main/java/com/umc/networkingService/domain/member/entity/MemberPoint.java @@ -1,7 +1,7 @@ package com.umc.networkingService.domain.member.entity; import com.umc.networkingService.domain.university.entity.University; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/com/umc/networkingService/domain/message/entity/Message.java b/src/main/java/com/umc/networkingService/domain/message/entity/Message.java index a3477c52..c3e57424 100644 --- a/src/main/java/com/umc/networkingService/domain/message/entity/Message.java +++ b/src/main/java/com/umc/networkingService/domain/message/entity/Message.java @@ -1,6 +1,6 @@ package com.umc.networkingService.domain.message.entity; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/com/umc/networkingService/domain/message/entity/MessageRoom.java b/src/main/java/com/umc/networkingService/domain/message/entity/MessageRoom.java index 55e7146e..7a16e150 100644 --- a/src/main/java/com/umc/networkingService/domain/message/entity/MessageRoom.java +++ b/src/main/java/com/umc/networkingService/domain/message/entity/MessageRoom.java @@ -1,6 +1,6 @@ package com.umc.networkingService.domain.message.entity; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/com/umc/networkingService/domain/project/entity/Project.java b/src/main/java/com/umc/networkingService/domain/project/entity/Project.java index 49d4b9c0..78f10da3 100644 --- a/src/main/java/com/umc/networkingService/domain/project/entity/Project.java +++ b/src/main/java/com/umc/networkingService/domain/project/entity/Project.java @@ -1,6 +1,6 @@ package com.umc.networkingService.domain.project.entity; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import com.umc.networkingService.global.common.Semester; import jakarta.persistence.*; import lombok.AccessLevel; diff --git a/src/main/java/com/umc/networkingService/domain/project/entity/ProjectHeart.java b/src/main/java/com/umc/networkingService/domain/project/entity/ProjectHeart.java index 988dab9c..f98823c4 100644 --- a/src/main/java/com/umc/networkingService/domain/project/entity/ProjectHeart.java +++ b/src/main/java/com/umc/networkingService/domain/project/entity/ProjectHeart.java @@ -1,7 +1,7 @@ package com.umc.networkingService.domain.project.entity; import com.umc.networkingService.domain.member.entity.Member; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.*; import lombok.Getter; import lombok.NoArgsConstructor; diff --git a/src/main/java/com/umc/networkingService/domain/project/entity/ProjectMember.java b/src/main/java/com/umc/networkingService/domain/project/entity/ProjectMember.java index 2f983d72..e8ec067f 100644 --- a/src/main/java/com/umc/networkingService/domain/project/entity/ProjectMember.java +++ b/src/main/java/com/umc/networkingService/domain/project/entity/ProjectMember.java @@ -1,7 +1,7 @@ package com.umc.networkingService.domain.project.entity; import com.umc.networkingService.domain.member.entity.Member; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import com.umc.networkingService.global.common.Part; import jakarta.persistence.*; import lombok.Getter; diff --git a/src/main/java/com/umc/networkingService/domain/proposal/entity/Proposal.java b/src/main/java/com/umc/networkingService/domain/proposal/entity/Proposal.java index f95423ce..e2da5788 100644 --- a/src/main/java/com/umc/networkingService/domain/proposal/entity/Proposal.java +++ b/src/main/java/com/umc/networkingService/domain/proposal/entity/Proposal.java @@ -1,7 +1,7 @@ package com.umc.networkingService.domain.proposal.entity; import com.umc.networkingService.domain.member.entity.Member; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/com/umc/networkingService/domain/proposal/entity/proposalImage.java b/src/main/java/com/umc/networkingService/domain/proposal/entity/proposalImage.java index a3e3dea9..15edf1a0 100644 --- a/src/main/java/com/umc/networkingService/domain/proposal/entity/proposalImage.java +++ b/src/main/java/com/umc/networkingService/domain/proposal/entity/proposalImage.java @@ -1,6 +1,6 @@ package com.umc.networkingService.domain.proposal.entity; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/com/umc/networkingService/domain/schedule/entity/Schedule.java b/src/main/java/com/umc/networkingService/domain/schedule/entity/Schedule.java index 9d53cd3c..e509a462 100644 --- a/src/main/java/com/umc/networkingService/domain/schedule/entity/Schedule.java +++ b/src/main/java/com/umc/networkingService/domain/schedule/entity/Schedule.java @@ -2,7 +2,7 @@ import com.umc.networkingService.domain.board.entity.HostType; import com.umc.networkingService.domain.member.entity.Member; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import com.umc.networkingService.global.common.Semester; import jakarta.persistence.*; import lombok.AccessLevel; diff --git a/src/main/java/com/umc/networkingService/domain/session/entity/Session.java b/src/main/java/com/umc/networkingService/domain/session/entity/Session.java index 81ae61ab..a21fd320 100644 --- a/src/main/java/com/umc/networkingService/domain/session/entity/Session.java +++ b/src/main/java/com/umc/networkingService/domain/session/entity/Session.java @@ -1,7 +1,7 @@ package com.umc.networkingService.domain.session.entity; import com.umc.networkingService.domain.member.entity.Member; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/com/umc/networkingService/domain/test/controller/TestController.java b/src/main/java/com/umc/networkingService/domain/test/controller/TestController.java index 3d31e869..45dc81ad 100644 --- a/src/main/java/com/umc/networkingService/domain/test/controller/TestController.java +++ b/src/main/java/com/umc/networkingService/domain/test/controller/TestController.java @@ -1,35 +1,60 @@ package com.umc.networkingService.domain.test.controller; +import com.umc.networkingService.domain.test.dto.TestRequest; +import com.umc.networkingService.domain.test.dto.TestResponse; +import com.umc.networkingService.domain.test.service.TestService; +import com.umc.networkingService.global.common.base.BaseResponse; import io.swagger.v3.oas.annotations.Hidden; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; -@Tag(name = "예제 API", description = "Swagger 테스트용 API") +@Tag(name = "예제 API", description = "테스트용 API") @RestController @RequiredArgsConstructor @RequestMapping("/test") public class TestController { - @Operation(summary = "문자열 반복", description = "파라미터로 받은 문자열을 2번 반복합니다.") - @Parameter(name = "str", description = "2번 반복할 문자열") - @GetMapping("/returnStr") - public String returnStr(@RequestParam String str) { - return str + "\n" + str; + private final TestService testService; + + + @Operation(summary = "성공적인 응답 반환 API", description = "테스트 문자열을 반환하는 API입니다.") + @ApiResponse(responseCode = "200", description = "테스트 문자열을 성공적으로 반환") + @GetMapping("/success") + public BaseResponse successResponseAPI() { + return BaseResponse.onSuccess(TestResponse.TempTestDTO.builder().testString("This is Test!").build()); } - @GetMapping("/example") - public String example() { - return "예시 API"; + + + @Operation(summary = "예외처리 API", description = "flag 값에 따라 예외를 발생시키는 API입니다.") + @Parameter(name = "flag", description = "1인 경우 예외처리", example = "1", required = true) + @ApiResponses(value = { + @ApiResponse(responseCode = "COMMON200", description = "성공"), + @ApiResponse(responseCode = "TEMP4001", description = "예외 처리 테스트입니다.") + }) + @GetMapping("/exception") + public BaseResponse exceptionAPI(@RequestParam Integer flag) { + testService.CheckFlag(flag); + return BaseResponse.onSuccess(TestResponse.TempExceptionDTO.builder() + .flag(flag) + .build()); } - @Hidden - @GetMapping("/ignore") - public String ignore() { - return "무시되는 API"; + @Operation(summary = "유효성 검사 API", description = "요청 객체의 유효성을 검사하는 API입니다.") + @ApiResponses(value = { + @ApiResponse(responseCode = "COMMON200", description = "성공"), + @ApiResponse(responseCode = "COMMON400", description = "testString가 공백일 경우") + }) + @PostMapping("/validation") + public BaseResponse testValidationAPI( + @RequestBody @Valid TestRequest.TempTestRequest request) { + return BaseResponse.onSuccess(TestResponse.TempTestDTO.builder() + .testString(request.getTestString()) + .build()); } } \ No newline at end of file diff --git a/src/main/java/com/umc/networkingService/domain/test/dto/TestRequest.java b/src/main/java/com/umc/networkingService/domain/test/dto/TestRequest.java new file mode 100644 index 00000000..918dd3dd --- /dev/null +++ b/src/main/java/com/umc/networkingService/domain/test/dto/TestRequest.java @@ -0,0 +1,19 @@ +package com.umc.networkingService.domain.test.dto; + +import jakarta.validation.constraints.NotBlank; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +public class TestRequest { + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class TempTestRequest { + @NotBlank(message = "testString은 필수입니다.") + private String testString; + } +} diff --git a/src/main/java/com/umc/networkingService/domain/test/dto/TestResponse.java b/src/main/java/com/umc/networkingService/domain/test/dto/TestResponse.java new file mode 100644 index 00000000..d2d9511a --- /dev/null +++ b/src/main/java/com/umc/networkingService/domain/test/dto/TestResponse.java @@ -0,0 +1,25 @@ +package com.umc.networkingService.domain.test.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +public class TestResponse { + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class TempTestDTO { + String testString; + } + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class TempExceptionDTO { + Integer flag; + } +} \ No newline at end of file diff --git a/src/main/java/com/umc/networkingService/domain/test/service/TestService.java b/src/main/java/com/umc/networkingService/domain/test/service/TestService.java new file mode 100644 index 00000000..d6d972dd --- /dev/null +++ b/src/main/java/com/umc/networkingService/domain/test/service/TestService.java @@ -0,0 +1,15 @@ +package com.umc.networkingService.domain.test.service; + +import com.umc.networkingService.global.common.exception.RestApiException; +import com.umc.networkingService.global.common.exception.ErrorCode; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class TestService { + public void CheckFlag(Integer flag) { + if (flag == 1) + throw new RestApiException(ErrorCode.TEMP_EXCEPTION); + } +} diff --git a/src/main/java/com/umc/networkingService/domain/todayILearned/entity/TodayILearned.java b/src/main/java/com/umc/networkingService/domain/todayILearned/entity/TodayILearned.java index f984b7c7..4bd6a9d4 100644 --- a/src/main/java/com/umc/networkingService/domain/todayILearned/entity/TodayILearned.java +++ b/src/main/java/com/umc/networkingService/domain/todayILearned/entity/TodayILearned.java @@ -1,7 +1,7 @@ package com.umc.networkingService.domain.todayILearned.entity; import com.umc.networkingService.domain.member.entity.Member; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import com.umc.networkingService.global.common.Part; import jakarta.persistence.*; import lombok.AccessLevel; diff --git a/src/main/java/com/umc/networkingService/domain/todayILearned/entity/TodayILearnedFile.java b/src/main/java/com/umc/networkingService/domain/todayILearned/entity/TodayILearnedFile.java index c986a0e4..ac7930f0 100644 --- a/src/main/java/com/umc/networkingService/domain/todayILearned/entity/TodayILearnedFile.java +++ b/src/main/java/com/umc/networkingService/domain/todayILearned/entity/TodayILearnedFile.java @@ -1,6 +1,6 @@ package com.umc.networkingService.domain.todayILearned.entity; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.*; import lombok.Getter; import lombok.NoArgsConstructor; diff --git a/src/main/java/com/umc/networkingService/domain/todoList/entity/ToDoList.java b/src/main/java/com/umc/networkingService/domain/todoList/entity/ToDoList.java index 312ed5d2..ddbe4acf 100644 --- a/src/main/java/com/umc/networkingService/domain/todoList/entity/ToDoList.java +++ b/src/main/java/com/umc/networkingService/domain/todoList/entity/ToDoList.java @@ -1,7 +1,7 @@ package com.umc.networkingService.domain.todoList.entity; import com.umc.networkingService.domain.member.entity.Member; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/com/umc/networkingService/domain/university/entity/University.java b/src/main/java/com/umc/networkingService/domain/university/entity/University.java index 42c563a7..a883006c 100644 --- a/src/main/java/com/umc/networkingService/domain/university/entity/University.java +++ b/src/main/java/com/umc/networkingService/domain/university/entity/University.java @@ -1,7 +1,7 @@ package com.umc.networkingService.domain.university.entity; import com.umc.networkingService.domain.mascot.entity.Mascot; -import com.umc.networkingService.global.common.BaseEntity; +import com.umc.networkingService.global.common.base.BaseEntity; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/com/umc/networkingService/global/common/BaseResponse.java b/src/main/java/com/umc/networkingService/global/common/BaseResponse.java deleted file mode 100644 index 9970fd77..00000000 --- a/src/main/java/com/umc/networkingService/global/common/BaseResponse.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.umc.networkingService.global.common; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonPropertyOrder; -import lombok.AllArgsConstructor; -import lombok.Getter; - -import java.time.LocalDateTime; - -@Getter -@AllArgsConstructor -@JsonPropertyOrder({"timestamp", "message", "result"}) -public class BaseResponse { - - private final LocalDateTime timestamp = LocalDateTime.now(); - - private final String message; - @JsonInclude(JsonInclude.Include.NON_NULL) - private T result; - - public BaseResponse(T result) { - this.message = "요청에 성공하였습니다."; - this.result = result; - } - -} - diff --git a/src/main/java/com/umc/networkingService/global/common/BaseEntity.java b/src/main/java/com/umc/networkingService/global/common/base/BaseEntity.java similarity index 93% rename from src/main/java/com/umc/networkingService/global/common/BaseEntity.java rename to src/main/java/com/umc/networkingService/global/common/base/BaseEntity.java index 8841bb51..5ea28b58 100644 --- a/src/main/java/com/umc/networkingService/global/common/BaseEntity.java +++ b/src/main/java/com/umc/networkingService/global/common/base/BaseEntity.java @@ -1,4 +1,4 @@ -package com.umc.networkingService.global.common; +package com.umc.networkingService.global.common.base; import jakarta.persistence.Column; import jakarta.persistence.EntityListeners; diff --git a/src/main/java/com/umc/networkingService/global/common/base/BaseResponse.java b/src/main/java/com/umc/networkingService/global/common/base/BaseResponse.java new file mode 100644 index 00000000..0b6c6771 --- /dev/null +++ b/src/main/java/com/umc/networkingService/global/common/base/BaseResponse.java @@ -0,0 +1,33 @@ +package com.umc.networkingService.global.common.base; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.time.LocalDateTime; + +@Getter +@AllArgsConstructor +@JsonPropertyOrder({"timestamp", "code", "message", "result"}) +public class BaseResponse { + + private final LocalDateTime timestamp = LocalDateTime.now(); + private final String code; + private final String message; + @JsonInclude(JsonInclude.Include.NON_NULL) + private T result; + + + //성공한 경우 응답 생성 + public static BaseResponse onSuccess(T result) { + return new BaseResponse<>("COMMON200", "요청에 성공하였습니다.", result); + } + + // 실패한 경우 응답 생성 + public static BaseResponse onFailure(String code, String message, T data) { + return new BaseResponse<>(code, message, data); + } + +} + diff --git a/src/main/java/com/umc/networkingService/global/common/exception/ErrorCode.java b/src/main/java/com/umc/networkingService/global/common/exception/ErrorCode.java new file mode 100644 index 00000000..323264ab --- /dev/null +++ b/src/main/java/com/umc/networkingService/global/common/exception/ErrorCode.java @@ -0,0 +1,28 @@ +package com.umc.networkingService.global.common.exception; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; + +@Getter +@RequiredArgsConstructor +public enum ErrorCode { + + /* + * 에러 + */ + // 가장 일반적인 응답 + _INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "COMMON500", "서버 에러, 관리자에게 문의 바랍니다."), + _BAD_REQUEST(HttpStatus.BAD_REQUEST, "COMMON400", "잘못된 요청입니다."), + _UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "COMMON401", "인증이 필요합니다."), + _VALIDATION_ERROR(HttpStatus.BAD_REQUEST, "COMMON402", "Validation Error입니다."), + _FORBIDDEN(HttpStatus.FORBIDDEN, "COMMON403", "금지된 요청입니다."), + _NOT_FOUND(HttpStatus.NOT_FOUND, "COMMON404", "요청한 정보를 찾을 수 없습니다."), + + // For test + TEMP_EXCEPTION(HttpStatus.BAD_REQUEST, "TEMP4001", "예외처리 테스트입니다."); + + private final HttpStatus httpStatus; + private final String code; + private final String message; +} diff --git a/src/main/java/com/umc/networkingService/global/common/exception/ExceptionAdvice.java b/src/main/java/com/umc/networkingService/global/common/exception/ExceptionAdvice.java new file mode 100644 index 00000000..baf0eafb --- /dev/null +++ b/src/main/java/com/umc/networkingService/global/common/exception/ExceptionAdvice.java @@ -0,0 +1,89 @@ +package com.umc.networkingService.global.common.exception; + +import com.umc.networkingService.global.common.base.BaseResponse; +import jakarta.validation.ConstraintViolationException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatusCode; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.context.request.WebRequest; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Optional; + +@Slf4j +@RestControllerAdvice(annotations = {RestController.class}) +public class ExceptionAdvice extends ResponseEntityExceptionHandler { + /* + * 직접 정의한 RestApiException 에러 클래스에 대한 예외 처리 + */ + @ExceptionHandler(value = RestApiException.class) + public ResponseEntity> handleRestApiException(RestApiException e) { + ErrorCode errorCode = e.getErrorCode(); + return handleExceptionInternal(errorCode); + } + + /* + * 일반적인 서버 에러에 대한 예외 처리 + */ + @org.springframework.web.bind.annotation.ExceptionHandler + public ResponseEntity> handleException(Exception e) { + e.printStackTrace(); //예외 정보 출력 + + return handleExceptionInternalFalse(ErrorCode._INTERNAL_SERVER_ERROR, e.getMessage()); + } + + /* + * ConstraintViolationException 발생 시 예외 처리 + * 메서드 파라미터, 또는 메서드 리턴 값에 문제가 있을 경우, @Validated 검증 실패한 경우 + */ + @org.springframework.web.bind.annotation.ExceptionHandler + public ResponseEntity> handleConstraintViolationException(ConstraintViolationException e) { + return handleExceptionInternal(ErrorCode._VALIDATION_ERROR); + } + + /* + * MethodArgumentNotValidException 발생 시 예외 처리 + * @@RequestBody 내부에서 처리 실패한 경우, @Valid 검증 실패한 경우 (ArgumnetResolver에 의해 유효성 검사) + */ + @Override + public ResponseEntity handleMethodArgumentNotValid( + MethodArgumentNotValidException e, HttpHeaders headers, HttpStatusCode statusCode, WebRequest request) { + Map errors = new LinkedHashMap<>(); + + e.getBindingResult().getFieldErrors().stream() + .forEach(fieldError -> { + String fieldName = fieldError.getField(); + String errorMessage = Optional.ofNullable(fieldError.getDefaultMessage()).orElse(""); + errors.merge(fieldName, errorMessage, (existingErrorMessage, newErrorMessage) -> existingErrorMessage + ", " + newErrorMessage); + }); + + return handleExceptionInternalArgs(ErrorCode.valueOf("_BAD_REQUEST"), errors); + + } + + + private ResponseEntity> handleExceptionInternal(ErrorCode errorCode) { + return ResponseEntity + .status(errorCode.getHttpStatus().value()) + .body(BaseResponse.onFailure(errorCode.getCode(), errorCode.getMessage(), null)); + } + + private ResponseEntity handleExceptionInternalArgs(ErrorCode errorCode, Map errorArgs) { + return ResponseEntity + .status(errorCode.getHttpStatus().value()) + .body(BaseResponse.onFailure(errorCode.getCode(), errorCode.getMessage(), errorArgs)); + } + + private ResponseEntity> handleExceptionInternalFalse(ErrorCode errorCode, String errorPoint) { + return ResponseEntity + .status(errorCode.getHttpStatus().value()) + .body(BaseResponse.onFailure(errorCode.getCode(), errorCode.getMessage(), errorPoint)); + } +} diff --git a/src/main/java/com/umc/networkingService/global/common/exception/RestApiException.java b/src/main/java/com/umc/networkingService/global/common/exception/RestApiException.java new file mode 100644 index 00000000..36acc5ac --- /dev/null +++ b/src/main/java/com/umc/networkingService/global/common/exception/RestApiException.java @@ -0,0 +1,11 @@ +package com.umc.networkingService.global.common.exception; + +import lombok.AllArgsConstructor; +import lombok.Getter; + + +@Getter +@AllArgsConstructor +public class RestApiException extends RuntimeException { + private final ErrorCode errorCode; +}