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();
}
}