如何“toString()”用于日志记录和调试的 GWT EntityProxy 衍生产品?

发布于 2024-10-14 10:20:25 字数 535 浏览 7 评论 0原文

GWT 2.1.1 有非常好的框架 - RequestFactory 以及所有 EntityProxy 和其他东西。

我正在寻找一种方法来序列化实现 EntityProxy 的运行时实例以进行调试和日志记录等。我不关心格式,只要它是人类可读的即可。 更具体地说,我想要类似 Apache Commons Lang 提供的东西 ReflectionToStringBuilder 可能有某种方法可以使用 GWT 内部的 JSON 序列化机制?如果是,如何使其更具可读性?

import org.apache.commons.lang.builder.ReflectionToStringBuilder;

String stringRep = ReflectionToStringBuilder.toString(this);

GWT 2.1.1 has very good framework - RequestFactory with all the EntityProxy and stuff.

I am looking for a way to serialize runtime instances that implement EntityProxy for debugging and logging etc. I do not care for format as long as it human readable.
To be more specific I would like to have something like the provided by Apache Commons Lang
ReflectionToStringBuilder
May be there is some way to use the JSON serialization mechanics that GWT has inside? if yes how to make it a bit more readable?

import org.apache.commons.lang.builder.ReflectionToStringBuilder;

String stringRep = ReflectionToStringBuilder.toString(this);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

明媚殇 2024-10-21 10:20:25

至少有两种解决方案:

第一:基于 Thomas Broyer 的想法,

public static String toString(EntityProxy entityProxy)
{
    DefaultProxyStore store = new DefaultProxyStore();
    Swap.requestFactory.getSerializer(store).serialize(entityProxy);
    return store.encode();
}

产生如下内容:

{"V":"211","P":{"1@[email protected]":{"O":"PERSIST","R":"2","Y":1,"T":"biz.daich.swap.shared.dto.UserAccountProxy","P":{"id":null,"items":null,"channelId":null,"lastActive":1296194777916,"name":null,"emailAddress":"[email protected]","lastReported":1296194777916,"lastLoginOn":1296194777916}}}}

第二:基于 AutoBean 框架

public static String toJson(EntityProxy entityProxy)
{
    return AutoBeanCodex.encode(AutoBeanUtils.getAutoBean(entityProxy)).getPayload();
}

它生成类似的字符串

{"emailAddress":"[email protected]","lastActive":1296194777916,"lastLoginOn":1296194777916,"lastReported":1296194777916}

第二个是正是我所需要的 - 它在日志中更具可读性。

There are at least 2 solutions:

First: Based on the idea by Thomas Broyer

public static String toString(EntityProxy entityProxy)
{
    DefaultProxyStore store = new DefaultProxyStore();
    Swap.requestFactory.getSerializer(store).serialize(entityProxy);
    return store.encode();
}

Which produce something like this:

{"V":"211","P":{"1@[email protected]":{"O":"PERSIST","R":"2","Y":1,"T":"biz.daich.swap.shared.dto.UserAccountProxy","P":{"id":null,"items":null,"channelId":null,"lastActive":1296194777916,"name":null,"emailAddress":"[email protected]","lastReported":1296194777916,"lastLoginOn":1296194777916}}}}

Second: Based on the AutoBean framework

public static String toJson(EntityProxy entityProxy)
{
    return AutoBeanCodex.encode(AutoBeanUtils.getAutoBean(entityProxy)).getPayload();
}

Which produce string like

{"emailAddress":"[email protected]","lastActive":1296194777916,"lastLoginOn":1296194777916,"lastReported":1296194777916}

The second is just what I need - it more readable in log.

始终不够爱げ你 2024-10-21 10:20:25

我还没有尝试过,但看看 RequestFactory#getSerializer,javadoc 中有一些示例代码 ProxySerializer

I haven't tried it but have a look at RequestFactory#getSerializer, there's some sample code in the javadoc for the ProxySerializer.

云裳 2024-10-21 10:20:25

如果使用该方法
toJson(EntityProxy entityProxy)

将其更改为

toJson(BaseProxy proxy),

然后您可以记录 Value 和 Entity Proxy 对象。

If using the method
toJson(EntityProxy entityProxy)

change this to

toJson(BaseProxy proxy)

and then you can log Value and Entity Proxy objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文