如何在 toString 方法上使用 Jackson 序列化?
How to use Jackson serialization on toString method?
我已经通过 Jackson
序列化设置了一个暴露的实体 class,这在我的 RestController
.
中运行良好
最近,我试图创建一个Spring AOP
来显示日志,但它使用了class的toString
,它抛出Whosebug
异常,因为class 包含双向关系。
Object result = joinPoint.proceed();
if (log.isDebugEnabled()) {
log.debug("Exit: {}.{}() with result = {}",
joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(),
result);
}
如何设置 toString
方法以使用已配置的 Jackson
序列化?
我使用 ObjectMapper
.
解决了我的问题
Object result = joinPoint.proceed();
if (log.isDebugEnabled()) {
log.debug("Exit: {}.{}() with result = {}",
joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(),
new ObjectMapper().writeValueAsString(result));
}
我已经通过 Jackson
序列化设置了一个暴露的实体 class,这在我的 RestController
.
最近,我试图创建一个Spring AOP
来显示日志,但它使用了class的toString
,它抛出Whosebug
异常,因为class 包含双向关系。
Object result = joinPoint.proceed();
if (log.isDebugEnabled()) {
log.debug("Exit: {}.{}() with result = {}",
joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(),
result);
}
如何设置 toString
方法以使用已配置的 Jackson
序列化?
我使用 ObjectMapper
.
Object result = joinPoint.proceed();
if (log.isDebugEnabled()) {
log.debug("Exit: {}.{}() with result = {}",
joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(),
new ObjectMapper().writeValueAsString(result));
}