Skip to content

Commit

Permalink
[Feature] BindingRequest 관련 예외처리 로직 AOP로 분리 (#337)
Browse files Browse the repository at this point in the history
* setting: AOP를 위한 의존성 추가

* feat: 중복 코드 제거를 위해 BindingRequest 처리 로직 AOP로 등록

* refactor: controller에서 bindingRequest관련 분기문 제거

* test: aop 적용을 위해 controller 테스트에 어노테이션 추가

* chore: BindingAdvice 위치 변경

---------

Co-authored-by: imdh <[email protected]>
Co-authored-by: jangdongho <[email protected]>
  • Loading branch information
3 people authored Aug 21, 2023
1 parent 2149938 commit 58b7468
Show file tree
Hide file tree
Showing 24 changed files with 120 additions and 124 deletions.
4 changes: 4 additions & 0 deletions BE-MyCarMaster/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-cache'


// aop
implementation 'org.springframework.boot:spring-boot-starter-aop'

// monitoring
implementation 'io.micrometer:micrometer-registry-prometheus'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import softeer.be_my_car_master.application.agency.dto.request.GetAgenciesRequest;
import softeer.be_my_car_master.application.agency.dto.response.GetAgenciesResponse;
import softeer.be_my_car_master.application.agency.usecase.get_agencies.GetAgenciesUseCase;
import softeer.be_my_car_master.global.exception.BindingParamException;
import softeer.be_my_car_master.global.response.Response;

@RestController
Expand All @@ -27,10 +26,6 @@ public Response<GetAgenciesResponse> getAgencies(
@Valid @ParameterObject GetAgenciesRequest request,
BindingResult bindingResult
) {
if (bindingResult.hasErrors()) {
throw new BindingParamException(bindingResult.getFieldErrors());
}

String gu = request.getGu();
GetAgenciesResponse response = getAgenciesUseCase.execute(gu);
return Response.createSuccessResponse(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import softeer.be_my_car_master.application.body_type.dto.request.GetBodyTypesRequest;
import softeer.be_my_car_master.application.body_type.dto.response.GetBodyTypesResponse;
import softeer.be_my_car_master.application.body_type.usecase.get_body_types.GetBodyTypesUseCase;
import softeer.be_my_car_master.global.exception.BindingParamException;
import softeer.be_my_car_master.global.response.Response;

@RestController
Expand All @@ -29,12 +28,8 @@ public Response<GetBodyTypesResponse> getBodyTypes(
@Valid @ParameterObject GetBodyTypesRequest request,
BindingResult bindingResult
) {
if (bindingResult.hasErrors()) {
throw new BindingParamException(bindingResult.getFieldErrors());
}

Long modelId = request.getModelId();
GetBodyTypesResponse getBodyTypesResponse = getBodyTypesUseCase.execute(modelId);
return Response.createSuccessResponse(getBodyTypesResponse);
GetBodyTypesResponse response = getBodyTypesUseCase.execute(modelId);
return Response.createSuccessResponse(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import softeer.be_my_car_master.application.car_master.dto.response.GetCarMasterResponse;
import softeer.be_my_car_master.application.car_master.usecase.get_car_masters.GetCarMasterUseCase;
import softeer.be_my_car_master.application.car_master.usecase.get_car_masters_in_agency.GetCarMastersInAgencyUseCase;
import softeer.be_my_car_master.global.exception.BindingParamException;
import softeer.be_my_car_master.global.response.Response;

@RestController
Expand All @@ -34,10 +33,6 @@ public Response<GetCarMasterResponse> getCarMasters(
@Valid @ParameterObject GetCarMasterRequest request,
BindingResult bindingResult
) {
if (bindingResult.hasErrors()) {
throw new BindingParamException(bindingResult.getFieldErrors());
}

Double latitude = request.getLatitude();
Double longitude = request.getLongitude();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import softeer.be_my_car_master.application.color_exterior.dto.request.GetExteriorColorsRequest;
import softeer.be_my_car_master.application.color_exterior.dto.response.GetExteriorColorsResponse;
import softeer.be_my_car_master.application.color_exterior.usecase.get_exterior_colors.GetExteriorColorsUseCase;
import softeer.be_my_car_master.global.exception.BindingParamException;
import softeer.be_my_car_master.global.response.Response;

@RestController
Expand All @@ -29,10 +28,6 @@ public Response<GetExteriorColorsResponse> getExteriorColors(
@Valid @ParameterObject GetExteriorColorsRequest request,
BindingResult bindingResult
) {
if (bindingResult.hasErrors()) {
throw new BindingParamException(bindingResult.getFieldErrors());
}

Long trimId = request.getTrimId();
GetExteriorColorsResponse response = getExteriorColorsUseCase.execute(trimId);
return Response.createSuccessResponse(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import softeer.be_my_car_master.application.color_interior.dto.request.GetInteriorColorsRequest;
import softeer.be_my_car_master.application.color_interior.dto.response.GetInteriorColorsResponse;
import softeer.be_my_car_master.application.color_interior.usecase.get_interior_colors.GetInteriorColorsUseCase;
import softeer.be_my_car_master.global.exception.BindingParamException;
import softeer.be_my_car_master.global.response.Response;

@RestController
Expand All @@ -29,10 +28,6 @@ public Response<GetInteriorColorsResponse> getInterior(
@Valid @ParameterObject GetInteriorColorsRequest request,
BindingResult bindingResult
) {
if (bindingResult.hasErrors()) {
throw new BindingParamException(bindingResult.getFieldErrors());
}

Long trimId = request.getTrimId();
Long exteriorColorId = request.getExteriorColorId();
GetInteriorColorsResponse response = getInteriorColorsUseCase.execute(trimId, exteriorColorId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import softeer.be_my_car_master.application.engine.dto.request.GetEnginesRequest;
import softeer.be_my_car_master.application.engine.dto.response.GetEnginesResponse;
import softeer.be_my_car_master.application.engine.usecase.get_engines.GetEnginesUseCase;
import softeer.be_my_car_master.global.exception.BindingParamException;
import softeer.be_my_car_master.global.response.Response;

@RestController
Expand All @@ -29,12 +28,8 @@ public Response<GetEnginesResponse> getEngines(
@Valid @ParameterObject GetEnginesRequest request,
BindingResult bindingResult
) {
if (bindingResult.hasErrors()) {
throw new BindingParamException(bindingResult.getFieldErrors());
}

Long trimId = request.getTrimId();
GetEnginesResponse getEnginesResponse = getEnginesUseCase.execute(trimId);
return Response.createSuccessResponse(getEnginesResponse);
GetEnginesResponse response = getEnginesUseCase.execute(trimId);
return Response.createSuccessResponse(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import softeer.be_my_car_master.application.option.usecase.get_default_options.GetDefaultOptionsUseCase;
import softeer.be_my_car_master.application.option.usecase.get_trim_default_options.GetTrimDefaultOptionsUseCase;
import softeer.be_my_car_master.application.trim.dto.response.GetTrimDefaultOptionsResponse;
import softeer.be_my_car_master.global.exception.BindingParamException;
import softeer.be_my_car_master.global.response.Response;

@RestController
Expand All @@ -33,10 +32,6 @@ public Response<GetDefaultOptionsResponse> getDefaultOptions(
@Valid @ParameterObject GetDefaultOptionsRequest request,
BindingResult bindingResult
) {
if (bindingResult.hasErrors()) {
throw new BindingParamException(bindingResult.getFieldErrors());
}

Long trimId = request.getTrimId();
Long engineId = request.getEngineId();
Long wheelDriveId = request.getWheelDriveId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import softeer.be_my_car_master.application.option.dto.response.GetOptionsResponse;
import softeer.be_my_car_master.application.option.usecase.get_options.GetOptionsUseCase;
import softeer.be_my_car_master.application.option.usecase.get_unselectable_options_by_engine.GetUnselectableOptionsByEngineUseCase;
import softeer.be_my_car_master.global.exception.BindingParamException;
import softeer.be_my_car_master.global.response.Response;

@RestController
Expand All @@ -36,10 +35,6 @@ public Response<GetOptionsResponse> getOptions(
@Valid @ParameterObject GetOptionsRequest request,
BindingResult bindingResult
) {
if (bindingResult.hasErrors()) {
throw new BindingParamException(bindingResult.getFieldErrors());
}

Long trimId = request.getTrimId();
Long engineId = request.getEngineId();
Long wheelDriveId = request.getWheelDriveId();
Expand All @@ -58,10 +53,6 @@ public Response<GetUnselectableOptionsByEngineResponse> getUnselectableOptionsBy
@Valid @ParameterObject GetUnselectableOptionsByEngineRequest request,
BindingResult bindingResult
) {
if (bindingResult.hasErrors()) {
throw new BindingParamException(bindingResult.getFieldErrors());
}

Long trimId = request.getTrimId();
List<Long> optionIds = request.getOptionIds();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import softeer.be_my_car_master.application.option.dto.request.GetRepresentativeOptionsRequest;
import softeer.be_my_car_master.application.option.dto.response.GetRepresentativeOptionsResponse;
import softeer.be_my_car_master.application.option.usecase.get_representative_options.GetRepresentativeOptionsUseCase;
import softeer.be_my_car_master.global.exception.BindingParamException;
import softeer.be_my_car_master.global.response.Response;

@RestController
Expand All @@ -29,10 +28,6 @@ public Response<GetRepresentativeOptionsResponse> getRepresentativeOptions(
@Valid @ParameterObject GetRepresentativeOptionsRequest request,
BindingResult bindingResult
) {
if (bindingResult.hasErrors()) {
throw new BindingParamException(bindingResult.getFieldErrors());
}

Long modelId = request.getModelId();

GetRepresentativeOptionsResponse response = getRepresentativeOptionsUseCase.execute(modelId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import softeer.be_my_car_master.application.trim.dto.request.GetTrimsRequest;
import softeer.be_my_car_master.application.trim.dto.response.GetTrimsResponse;
import softeer.be_my_car_master.application.trim.usecase.get_trims.GetTrimsUseCase;
import softeer.be_my_car_master.global.exception.BindingParamException;
import softeer.be_my_car_master.global.response.Response;

@RestController
Expand All @@ -29,10 +28,6 @@ public Response<GetTrimsResponse> getTrims(
@Valid @ParameterObject GetTrimsRequest request,
BindingResult bindingResult
) {
if (bindingResult.hasErrors()) {
throw new BindingParamException(bindingResult.getFieldErrors());
}

Long modelId = request.getModelId();
GetTrimsResponse getTrimsResponse = getTrimsUseCase.execute(modelId);
return Response.createSuccessResponse(getTrimsResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import softeer.be_my_car_master.application.wheeldrive.dto.request.GetWheelDrivesRequest;
import softeer.be_my_car_master.application.wheeldrive.dto.response.GetWheelDrivesResponse;
import softeer.be_my_car_master.application.wheeldrive.usecase.get_wheel_drives.GetWheelDrivesUseCase;
import softeer.be_my_car_master.global.exception.BindingParamException;
import softeer.be_my_car_master.global.response.Response;

@RestController
Expand All @@ -29,10 +28,6 @@ public Response<GetWheelDrivesResponse> getWheelDrives(
@Valid @ParameterObject GetWheelDrivesRequest request,
BindingResult bindingResult
) {
if (bindingResult.hasErrors()) {
throw new BindingParamException(bindingResult.getFieldErrors());
}

Long trimId = request.getTrimId();
Long engineId = request.getEngineId();
GetWheelDrivesResponse response = getWheelDrivesUseCase.execute(trimId, engineId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package softeer.be_my_car_master.global.exception;

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

@Aspect
@Component
public class BindingAdvice {

@Around("execution(* softeer.be_my_car_master.application..*Controller.*(.., @javax.validation.Valid (*), ..))")
public Object validParamRequest(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object[] args = proceedingJoinPoint.getArgs();

for (Object arg : args) {
if (arg instanceof BindingResult) {
BindingResult bindingResult = (BindingResult)arg;
if (bindingResult.hasErrors()) {
throw new BindingParamException(bindingResult.getFieldErrors());
}
}
}
return proceedingJoinPoint.proceed();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package softeer.be_my_car_master.application.agency;
package softeer.be_my_car_master.application.agency.controller;

import static org.mockito.BDDMockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
Expand All @@ -11,18 +11,22 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;

import com.fasterxml.jackson.databind.ObjectMapper;

import softeer.be_my_car_master.application.agency.controller.AgencyController;
import softeer.be_my_car_master.application.agency.dto.response.AgencyInGuDto;
import softeer.be_my_car_master.application.agency.dto.response.GetAgenciesResponse;
import softeer.be_my_car_master.application.agency.usecase.get_agencies.GetAgenciesUseCase;
import softeer.be_my_car_master.global.exception.BindingAdvice;
import softeer.be_my_car_master.global.response.Response;

@WebMvcTest(AgencyController.class)
@EnableAspectJAutoProxy
@Import(BindingAdvice.class)
@DisplayName("Agency Controller Test")
class AgencyControllerTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
Expand All @@ -22,10 +24,13 @@
import softeer.be_my_car_master.application.body_type.dto.response.BodyTypeDto;
import softeer.be_my_car_master.application.body_type.dto.response.GetBodyTypesResponse;
import softeer.be_my_car_master.application.body_type.usecase.get_body_types.GetBodyTypesUseCase;
import softeer.be_my_car_master.global.exception.BindingAdvice;
import softeer.be_my_car_master.global.response.Response;
import softeer.be_my_car_master.global.response.ResponseStatus;

@WebMvcTest(BodyTypeController.class)
@EnableAspectJAutoProxy
@Import(BindingAdvice.class)
@DisplayName("BodyType Controller Test")
class BodyTypeControllerTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
Expand All @@ -27,9 +29,12 @@
import softeer.be_my_car_master.application.car_master.dto.response.GetCarMasterResponse;
import softeer.be_my_car_master.application.car_master.usecase.get_car_masters.GetCarMasterUseCase;
import softeer.be_my_car_master.application.car_master.usecase.get_car_masters_in_agency.GetCarMastersInAgencyUseCase;
import softeer.be_my_car_master.global.exception.BindingAdvice;
import softeer.be_my_car_master.global.response.Response;

@WebMvcTest(CarMasterController.class)
@EnableAspectJAutoProxy
@Import(BindingAdvice.class)
@DisplayName("CarMaster Controller Test")
class CarMasterControllerTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
Expand All @@ -21,10 +23,13 @@
import softeer.be_my_car_master.application.color_exterior.dto.response.ExteriorColorDto;
import softeer.be_my_car_master.application.color_exterior.dto.response.GetExteriorColorsResponse;
import softeer.be_my_car_master.application.color_exterior.usecase.get_exterior_colors.GetExteriorColorsUseCase;
import softeer.be_my_car_master.global.exception.BindingAdvice;
import softeer.be_my_car_master.global.response.Response;
import softeer.be_my_car_master.global.response.ResponseStatus;

@WebMvcTest(ExteriorColorController.class)
@EnableAspectJAutoProxy
@Import(BindingAdvice.class)
@DisplayName("Exterior Color Controller Test")
class ExteriorColorControllerTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
Expand All @@ -22,10 +24,13 @@
import softeer.be_my_car_master.application.color_interior.dto.response.GetInteriorColorsResponse;
import softeer.be_my_car_master.application.color_interior.dto.response.InteriorColorDto;
import softeer.be_my_car_master.application.color_interior.usecase.get_interior_colors.GetInteriorColorsUseCase;
import softeer.be_my_car_master.global.exception.BindingAdvice;
import softeer.be_my_car_master.global.response.Response;
import softeer.be_my_car_master.global.response.ResponseStatus;

@WebMvcTest(InteriorColorController.class)
@EnableAspectJAutoProxy
@Import(BindingAdvice.class)
@DisplayName("Interior Color Controller Test")
class InteriorColorControllerTest {

Expand Down
Loading

0 comments on commit 58b7468

Please sign in to comment.