I tried to use @Aspect for logging all request and response. If my endpoint has @RequestBody my code is working, but my get endpoints has not @RequestBody and I can't see logs. is that any explanation for this situation?
My class like that;
@Aspect
@Component
@Slf4j
@RequiredArgsConstructor(onConstructor = @__({@Autowired, @NotNull}))
public class AspectLogging {
private final ObjectMapper objectMapper;
@Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping) || @annotation(org.springframework.web.bind.annotation.PostMapping)")
public void annotationPointCutDefinition() {
}
@Pointcut("execution(* *(com.dux.secondwallet.api.v3.pay.merchant.*))")
public void atExecution() {
}
@Before("annotationPointCutDefinition() && atExecution()")
public void endpointBefore(JoinPoint p) {
Object[] signatureArgs = p.getArgs();
if (Objects.nonNull(signatureArgs) && signatureArgs.length > 0) {
log.info("Request object: " + signatureArgs[0]);
}
}
@AfterReturning(pointcut = "annotationPointCutDefinition() && atExecution()", returning = "returnValue")
public void endpointAfterReturning(Object returnValue) {
try {
log.info("Response object:" + objectMapper.writeValueAsString(returnValue));
} catch (JsonProcessingException e) {
log.error(e.getMessage(), e);
}
}
@AfterThrowing(pointcut = "annotationPointCutDefinition() && atExecution()", throwing = "e")
public void endpointAfterThrowing(JoinPoint p, Exception e) throws Exception {
e.printStackTrace();
log.error(p.getTarget().getClass().getSimpleName() + " " + p.getSignature().getName() + " " + e.getMessage());
}
}
Example controller; getRequest method not logging, postRequest logging.
@Slf4j
@RestController
@RequestMapping("/v3/")
public class MyController {
@GetMapping("/balances")//not before and after logging
public List<java.lang.String> getRequest() {
return Collections.singletonList("TEST");
}
@PostMapping("/limits")//its logging
public TransactionLimitResponse postRequest(@Valid @RequestBody TransactionLimitRequest transactionLimitRequest) {
return TransactionLimitResponse.builder()
.currency("EUR")
.type("TYPE")
.min(100)
.max(1000)
.build();
}
}
First of all I want to ask you what do you want to log?
the purpose of your aspect code is to log method parameters, And You don't have any parameter on @GetMapping method.
so your aspect method is successfully triggered for @GetMapping too. But just check if condition and pass it. It is quite normal that you can't see logs.
apply the changes below it will work:
@Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)")
//@Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping) || @annotation(org.springframework.web.bind.annotation.PostMapping))")
public void getMapping() {
}
@Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)")
//@Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping) || @annotation(org.springframework.web.bind.annotation.PostMapping))")
public void postMapping() {
}
@Pointcut("execution(* *(..)) && within(com.dux.secondwallet.api.v3.pay.merchant.*))")
public void atExecution() {
}
@Before("(getMapping() || postMapping()) && atExecution()")
public void endpointBefore(JoinPoint p) {
log.info("ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss");
Object[] signatureArgs = p.getArgs();
if (Objects.nonNull(signatureArgs) && signatureArgs.length > 0) {
log.info("Request object: " + signatureArgs[0]);
}else{
log.info("log for get");
}
}
execution(* *(..))
: this is for your method signature.
within(com.dux.secondwallet.api.v3.pay.merchant.*)
this is package restriction.