[英]How to use fasterxml Jackson JSON serialization in Spring 4
[英]How to use Jackson serialization on toString method?
我已經通過Jackson
序列化設置了一個公開的實體類,它在我的RestController
運行良好。
最近,我嘗試創建一個Spring AOP
來顯示日志,但是它使用了類的toString
,它拋出了StackOverflow
異常,因為該類包含雙向關系。
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));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.