模式:从轴对象进行 Java 类转换

发布于 2024-07-08 03:36:23 字数 136 浏览 10 评论 0原文

我使用轴自动生成 Web 服务工件,然后将其转换为应用程序中使用的对象。 这样做有一个明智的模式吗? 我们编写了转换方法来从轴创建的对象输出我们的对象,有时我们编写了一组中间转换器类,将轴对象转换为我们的应用程序对象? 有没有通用的方法来解决这个问题?

I use a axis to auto-generate webservice artifacts which I then convert into objects used within our application. Is there a sensible pattern for doing this ? We have written transform methods to output our objects from the axis created objects, at other times we have written an intermediate set of transformer classes that transform the axis objects to our application objects? Is there a common way of approaching this?

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

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

发布评论

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

评论(5

浮世清欢 2024-07-15 03:36:24

如果我理解正确的话,您希望使用 Axis WSDL2Java 功能从 WSDL 生成代码,然后将其映射到现有的应用程序对象模型。 在这种情况下,您可能需要考虑将 Axis 与 JiBX 绑定一起使用。

If I understand correctly, you want to use Axis WSDL2Java feature to generate code from a WSDL and then map that to your existing application object model. In that case, you might want to consider to use Axis with JiBX binding.

甩你一脸翔 2024-07-15 03:36:24

如果目标是将“生成的轴”转换为您的业务对象,您可以尝试像 dozer 这样的工具(http:// dozer.sourceforge.net/)是一个“映射器”,用于从一个对象实现复制到另一个实现。
它不知道如何使用它(我认为你必须在 xml 文件中解释翻译),然后它应该可以工作(我自己没有使用它,但一些同事将它用于类似的目的,并且它似乎可以工作)

If the goal is to translate the "axis generated" to your business objects you can try a tool like dozer (http://dozer.sourceforge.net/) that is a "mapper" to copy from an object implementation to another implementation.
It don't know how to use it (I think you have to explain the translation in xml files) and then it should work (I didn't used myself but some colleagues used it for a similar purpose and it seemed to work)

我想有一种常识可以做到这一点。 抽象一下你的思想并想象一下层次。

  1. 您将获得一个具有基本 IO 以及基本会话功能和/或凭据的库。
  2. 创建一个转换层。 您将在这里放置构建应用程序相关对象所需的所有代码。 为您的应用程序提供一个漂亮的界面来完成这一层。

转换层可以通过多种方式完成。

  • 如果转换简单,您可以将第一层和第二层几乎合并为一个。 扩展 POJOS 以提供转换功能。 这将为您提供至少两段代码,自动生成的代码和扩展+功能

  • 如果我们正在谈论需要转换为多个小型应用程序对象的巨大 XML。 好的,保留第一层不变,在第二次使用时:

    • 命令模式:封装转换。
    • 如果转换也变得混乱:责任链将帮助您生成转换树。

我希望这有帮助

I guess there is a common sense way of doing that. Abstract a bit your mind and imagine layers.

  1. You get a library with the basic IO and perhaps basic session functionality and/or credentials.
  2. Create a convert layer. Here you will place all the code needed to build your application dependent objects. Finish this layer with a nice interface for your application.

The convertion layer can be done in multiple ways.

  • If the conversion is simple, you can merge the first and second layer almost in one. Extend the POJOS to provide transformation functionality. This will give you, at least two pieces of code, the automatically generated and the extensions + functionality

  • If we're talking about huge XMLs that need to be converted to multiple small application objects. Ok, leave the first layer as it is and in the second use:

    • Command pattern: to encapsulate the transformations.
    • If the transformation also gets messy: Chain of responsibility will help you to generate a tree of transformations.

I hope this helps

轮廓§ 2024-07-15 03:36:24

我们过去也遇到过类似的问题。 最近,对于我们让 Axis 为其生成工件的复杂 Web 服务,我们最终得到了 157 个 Java 类,其名称类似于“MaintainOffersRequestTypeReqReqDataMaintEnhancementCancellationReason”。

我们最终编写了一些类,将这些 Axis 生成的对象转换为业务对象,类似于您的上一个想法。 我不知道这是否是最好的方法,但这肯定是一个可行的解决方案。

We have run into similar issues in the past. Recently, for a complex web service that we had Axis generate artifacts for, we wound up with 157 Java classes with names like "MaintainOffersRequestTypeReqReqDataMaintEnhancementCancellationReason".

We wound up writing classes that transform these Axis-generated objects into business objects, similar to your last idea. I don't know if it's the best way, but it's certainly a workable solution.

守望孤独 2024-07-15 03:36:24

如果您使用的是 Axis 1,您的业务类与 Axis 数据类具有相同的限定名称,并且适用一些其他条件,则您只需使用您的业务类来代替 Axis 业务类,无需进行任何设置。 我们已经这样做过很多次了。

对于 Axis 2,我们在 ADB(默认)以外的数据绑定方面遇到了麻烦,因此我们广泛使用 Apache Commons Beanutils 在 Axis 类和业务类之间来回复制数据。

If you're using Axis 1, your business classes have the same qualified name as the Axis data class, and some other conditions apply, you can just use your business classes in place of the Axis ones with no setup needed. We've done that many times.

For Axis 2, we've had trouble with data bindings other than ADB (the default), so we use Apache Commons Beanutils extensively to copy data back and forth between Axis classes and our business classes.

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