I use AspectJ for logging controller and service class. I need to pass @RequestBody
field to my advice method, but I can not find the right way. There are some solutions but none of them works for me.
How to log request body using spring boot and aspect
Here is my REST controller:
public ResponseEntity<Employee> add(@RequestBody Employee body) {
//do something
}
Employee class:
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
public Integer id;
public String name;
}
My aspect:
@Before(value = "execution(* ir.co.example.controllers.EmployeeController.*(..))")
public void beforeAdvice(JoinPoint joinPoint) {
//do logging
}
I tried to get @RequestBody
as args()
in @Before
, for example:
@Before(value = "execution(* ir.co.example.controllers.EmployeeController.*(..)) && args(body)")
public void beforeAdvice(JoinPoint joinPoint, Employee body) {
System.out.println("User whit id = {} enter ", body.getId());
}
but beforeAdvice()
method does not call before my controller class.
I also tried this approach:
@Before(value = "execution(* ir.co.example.controllers.EmployeeController.*(..))")
public void beforeAdvice(JoinPoint joinPoint) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
//do logging
}
But in this solution I can not retrieve body. (I need Employee
.)
How can I solve this problem?