简体   繁体   English

如何在toString方法上使用Jackson序列化?

[英]How to use Jackson serialization on toString method?

I have already set up an exposed entity class through Jackson serialisation, which works well in my RestController . 我已经通过Jackson序列化设置了一个公开的实体类,它在我的RestController运行良好。

Recently, I tried to create a Spring AOP to display the log, but it uses toString of the class, which throws StackOverflow exception since the class contains a bi-directional relationship. 最近,我尝试创建一个Spring AOP来显示日志,但是它使用了类的toString ,它抛出了StackOverflow异常,因为该类包含双向关系。

Object result = joinPoint.proceed();
if (log.isDebugEnabled()) {
    log.debug("Exit: {}.{}() with result = {}", 
        joinPoint.getSignature().getDeclaringTypeName(),
        joinPoint.getSignature().getName(), 
        result);
}

How do I set up the toString method to use already configured Jackson serialisation? 如何设置toString方法以使用已配置的Jackson序列化?

I solved my problem by using ObjectMapper . 我通过使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM