Extracting response body in aspect for Spring boot application

1k views Asked by At

I would like to log response body with aspect in my Spring Boot application. As of now, I am doing this as follows:

 @Pointcut("within(com.web.rest.*)")
  public void applicationResourcePointcut() {
  }

 
  @AfterReturning(value = ("applicationResourcePointcut()"),
      returning = "returnValue")
  public void endpointAfterReturning(JoinPoint joinPoint, Object returnValue)  throws Throwable {
    
    try {
      System.out.println("RESPONSE OBJECT = " + mapper.writeValueAsString(returnValue)); 
    } catch (JsonProcessingException e) {
      System.out.println(e.getMessage());
    }
  }

But here, I am getting full response with Http Status code and other meta data as follows:

RESPONSE OBJECT = {"headers":{"Location":["/api/students/de7cc0b7-dcdf-4f2e-bc26-41525064dd55"],"X-ThreatModelSvc-Alert":["Entit ystudent is created with id:de7cc0b7-dcdf-4f2e-bc26-41525064dd55"]},"body":{"id":"de7cc0b7-dcdf-4f2e-bc26-41525064dd55","name":"Test Name","description":"Test Description","lastModifiedBy":"amallik"},"statusCodeValue":201,"statusCode":"CREATED"}

Here, I just would like to capture the response body. I cannot understand how to extract just the body from the response.

0

There are 0 answers