Search code examples
javaspring-mvcspring-aop

When use @Around aspect then RestController return empty response


I have simple rest controller :

    @RestController
    @RequestMapping(value = "/api/admin/operations")
    public class OperationController
    {
    
   @GetMapping("/get")
    public ResponseEntity<String> testGet(){
        //return new ResponseEntity(new BaseResponse<String>(200,"Succes","I am Data"), HttpStatus.OK);
        return new ResponseEntity<String>("Hello", HttpStatus.OK);

    }

and also I have @Aspect around for this controller:

@Aspect
@Configuration
public class UserActivityLogAspect
{

    @Autowired
    UserAuthorizationServiceImpl userAuthService;

    @Resource
    LoggingService loggingService;

    private static final org.apache.logging.log4j.Logger logger = LogManager.getLogger(UserActivityLogAspect.class);


    @Around("com.project.dsps.core.service.impl.common.aspect.CommonJoinPointConfig.operationControllerMethods()")
    public void aroundOperationUpdates(ProceedingJoinPoint joinPoint)
    {
        Signature signature = joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        OperationDTO operationDTO = null;
        UserActivityLogDto activityLogDto = new UserActivityLogDto();
        UserInfo userAuthData = new UserInfo();
        List<String> methodArgsStr = new ArrayList<>();
        Map<String, Object> methodArgsMap = new HashMap<>();
        RetryMicroOperationDTO retryMicroOperationDTO = null;
        String userRemoteAdd = "";
        StringBuilder httpRequestUri = new StringBuilder("");
        HttpServletRequest httpServletRequest = null;


        try
        {
            Object returnValue = joinPoint.proceed();
        }

        catch (Exception e)
        {
            e.printStackTrace();
            loggingService.userLog(this.getClass().getSuperclass().getSimpleName() + "." +
                                   signature.toShortString().replaceAll("\\((.+?)\\)", "") + "." + e.getMessage(),
                                   activityLogDto);
        }
        catch (Throwable throwable)
        {
            throwable.printStackTrace();
        }


        try
        {

            for (int i = 0; i < args.length; i++)
            {
                if (args[i] instanceof OperationDTO)
                {
                    operationDTO = (OperationDTO) args[i];
                }
                if (args[i] instanceof String)
                {
                    methodArgsStr.add((String) args[i]);
                }
                if (args[i] instanceof Map)
                {
                    methodArgsMap = (Map<String, Object>) args[i];
                }
                if (args[i] instanceof RetryMicroOperationDTO)
                {
                    retryMicroOperationDTO = (RetryMicroOperationDTO) args[i];
                }
                if (args[i] instanceof HttpServletRequest)
                {
                    httpServletRequest = (HttpServletRequest) args[i];
                    userRemoteAdd = httpServletRequest.getRemoteAddr();
                    httpRequestUri.append(httpServletRequest.getRequestURI().substring(0, 22));
                    httpRequestUri.append(signature.getName().toString());
                }
            }
            userAuthData.setAuthorizationToken((String) methodArgsMap.get("authorization"));
            userAuthData.setRemoteAddr(userRemoteAdd);
            activityLogDto.setUserInfo(userAuthData);

            methodArgsMap.put("path", httpRequestUri.toString());
            if (httpServletRequest != null)
            {
                methodArgsMap.put("full-uri", httpServletRequest.getRequestURI());
            }
            if (operationDTO != null)
            {
                ObjectMapper mapper = new ObjectMapper();
                JsonNode jsonNode = mapper.valueToTree(operationDTO);
                methodArgsMap.put(operationDTO.getClass().getSimpleName(), jsonNode);
            }
            if (retryMicroOperationDTO != null)
            {
                ObjectMapper mapper = new ObjectMapper();
                JsonNode jsonNode = mapper.valueToTree(retryMicroOperationDTO);
                methodArgsMap.put(retryMicroOperationDTO.getClass().getSimpleName(), jsonNode);
            }

            activityLogDto.setReqData(methodArgsMap);

            try
            {
                userAuthData = userAuthService.getUserAuthorization((String) methodArgsMap.get("authorization"));
                userAuthData.setAuthorizationToken((String) methodArgsMap.get("authorization"));
                userAuthData.setRemoteAddr(userRemoteAdd);
                activityLogDto.setUserInfo(userAuthData);

                loggingService.userLog(this.getClass().getSuperclass().getSimpleName() + "." +
                                       signature.toShortString().replaceAll("\\((.+?)\\)", ""), activityLogDto);
            }
            catch (UnauthorizedException e)
            {

            }
            catch (Exception e)
            {

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

when I call this rest API in Postman :

http://localhost:10001/api/admin/operations/get

I get a nothing response or error in method response,

But when I comment @Around annotation in UserActivityLogAspect , I getting response. also I would like add this that I debug methods execution for any exception or error that I nothing found any things and aspect method work properly .


Solution

  • You should return a result from your @Around advice.

        @Around("com.project.dsps.core.service.impl.common.aspect.CommonJoinPointConfig.operationControllerMethods()")
        public Object aroundOperationUpdates(ProceedingJoinPoint point) throws Throwable {
        // before method execution
        var result = point.proceed();
        // after method execution
        return result;
        }