I've added AOP to my little SpringBoot app to log inputs and outputs of the all methods:
@Aspect
@Component
@AllArgsConstructor
public class MyAspect{
private final ObjectMapper mapper;
@Pointcut("within(com.mypackage..*)")
protected void all() {}
@Before("all()")
public void before(final JoinPoint joinPoint) {
final Class<?> classData = joinPoint.getTarget().getClass();
final MethodSignature signature = (MethodSignature) joinPoint.getSignature();
final Map<Object, Object> parameters = this.getParameters(joinPoint);
try {
log.info(
"Class {} Method{} Parameters: {}",
classData.getSimpleName(),
signature.getName(),
this.mapper.writeValueAsString(parameters)
);
}
catch (final JsonProcessingException e) {
log.error("Error in JSON mapper", e);
}
}
@AfterReturning(value = "all()", returning = "returning")
public void after(final JoinPoint joinPoint, final Object returning) {
final Class<?> classData = joinPoint.getTarget().getClass();
final MethodSignature signature = (MethodSignature) joinPoint.getSignature();
try {
log.info(
"Class {} Method {} Returning {}",
classData.getSimpleName(),
signature.getName(),
this.mapper.writeValueAsString(returning)
);
}
catch (final JsonProcessingException e) {
log.error("Error in JSON mapper", e);
}
}
}
I have had a recursion problem in mapper.writeValueAsString(...). When I have an object with an attribute that inside has the same parent object (JPA relationship for example) it enters an infinite loop when it tries to deserialize this tree of objects. I have solved this problem by adding @JsonIgnoreProperties(value = {"entityParent"}) in the child entity, so that it does not deserialize the parent object again.
My current problem is that there are objects from Spring, from the framework, that I cannot edit to add this JSON property and they are giving me the same problem when deserializing.
My question is, how can I globally ignore this recursion for all objects?