Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/#12 응답 데이터 형식 및 전역 예외처리 추가 #14

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.team8.project2.global.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import com.team8.project2.global.dto.RsData;

import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;

@Aspect
@Component
@RequiredArgsConstructor
public class ResponseAspect {

private final HttpServletResponse response;

@Around("""
(
within
(
@org.springframework.web.bind.annotation.RestController *
)
&&
(
@annotation(org.springframework.web.bind.annotation.GetMapping)
||
@annotation(org.springframework.web.bind.annotation.PostMapping)
||
@annotation(org.springframework.web.bind.annotation.PutMapping)
||
@annotation(org.springframework.web.bind.annotation.DeleteMapping)
)
)
||
@annotation(org.springframework.web.bind.annotation.ResponseBody)
""")
public Object responseAspect(ProceedingJoinPoint joinPoint) throws Throwable {
Object rst = joinPoint.proceed();

if(rst instanceof RsData rsData) {
int statusCode = rsData.getStatusCode();
response.setStatus(statusCode);
}
return rst;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.team8.project2.global.dto;

public class Empty {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.team8.project2.global.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RsData<T> {
private String code;
private String msg;
private T data;

public RsData(String code, String msg) {
this(code, msg, (T)new Empty());
}

@JsonIgnore
public int getStatusCode() {
String statusCodeStr = code.split("-")[0];
return Integer.parseInt(statusCodeStr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.team8.project2.global.exception;

import java.util.stream.Collectors;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.team8.project2.global.dto.Empty;
import com.team8.project2.global.dto.RsData;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<RsData<Empty>> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {

String message = e.getBindingResult().getFieldErrors()
.stream()
.map(fe -> fe.getField() + " : " + fe.getCode() + " : " + fe.getDefaultMessage())
.sorted()
.collect(Collectors.joining("\n"));

return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(
new RsData<>(
"400-1",
message
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.team8.project2.global.exception;

import com.team8.project2.global.dto.RsData;

public class ServiceException extends RuntimeException {

private RsData<?> rsData;

public ServiceException(String code, String message) {
super(message);
rsData = new RsData<>(code, message);
}

public String getCode() {
return rsData.getCode();
}

public String getMsg() {
return rsData.getMsg();
}

public int getStatusCode() {
return rsData.getStatusCode();
}
}