wsimport - 如何在单独的项目/文件夹中生成服务端点类和 JAXB 类

发布于 2024-12-17 06:02:26 字数 327 浏览 8 评论 0原文

我们对具有多个 Web 服务(多个 WSDL)的项目使用自上而下的方法。每个 Web 服务都需要设置为单独的项目并部署为单独的 war。

问题在于 WSDL 共享一些常见的 .xsd 文件。目前,如果我们为每个 WSDL 运行 wsimport,则每个 Web 服务项目中都会复制公共 JAXB 类。

理想情况下,我们希望在公共共享项目中单独生成 JAXB 类,然后在每个 Web 服务项目中重用 JAXB 类项目,但 wsimport 不提供跳过 JAXB 类生成或指定不同位置的选项对于 JAXB 类。

关于如何在不同的 JAX-WS Web 服务端点之间共享 JAXB 类有什么想法吗?

We are using a top-down approach for a project with multiple web services (multiple WSDL's). Each web service needs to be set up as a separate project and deployed as a separate war.

The problem is that the WSDL's share a few common .xsd files. Currently if we run wsimport for each WSDL, the common JAXB classes are being duplicated in each web service project.

Ideally we would want to generate the JAXB classes separately in a common shared project, and then reuse the JAXB classes project in each of the web service projects, but wsimport does not provide the option to skip the JAXB class generation OR to specify a different location for the JAXB classes.

Any thoughts on how I can share the JAXB classes between different JAX-WS web service endpoints?

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

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

发布评论

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

评论(4

鹿! 2024-12-24 06:02:26

我知道这个问题很老了,但我想为那些正在寻找的人分享答案。我知道我花了一段时间才找到答案。

从 JAXB 2.1 RI 开始,有一个称为“episodes”的功能,您可以使用它来实现这一点。

假设您有一个名为 myschema.xsd 的架构。然后您需要调用以下命令:

xjc -episode myschema.episode myschema.xsd

如果您使用单个调用编译多个 xsd 文件,这也适用。该调用将生成绑定以及 myschema.episode 文件。

剧集文件是一个特殊的绑定文件。然后,您可以将此文件与 wsimport 一起使用,如下所示:

wsimport mywsdl.wsdl -b myschema.episode

wsimport 现在将使用先前生成的 JAXB 文件,并将生成缺少的任何内容。

请参阅此页面了解更多信息。

I know that this question is very old, but I wanted to share the answer for those that are looking. I know it took me a while to find the answer.

As of JAXB 2.1 RI, there's a feature called "episodes" that you can use to facilitate this.

Let's say you have a schema called myschema.xsd. Then you would want to call the following:

xjc -episode myschema.episode myschema.xsd

This also works if you are compiling multiple xsd files using a single call. The call will produce the bindings as well as the myschema.episode file.

The episode file is a special bindings file. You can then use this file with wsimport, like so:

wsimport mywsdl.wsdl -b myschema.episode

wsimport will now use the previously generated JAXB files, and will generate anything that is missing.

See this page for more information.

梦幻之岛 2024-12-24 06:02:26

您可以使用 JAXB/JAX-WS 自定义。假设您将 XSD 类型嵌入到 WSDL 中。那么您的定制将如下所示:

<jaxws:bindings version="2.0"
    xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    wsdlLocation="../wsdl/some.wsdl">

    <jaxws:package name="org.company.project.ws" />

    <!-- XSD types customization within WSDL -->
    <jaxb:bindings node="//xsd:schema">
        <jaxb:schemaBindings>
            <jaxb:package name="org.company.project.beans" />
        </jaxb:schemaBindings>
    </jaxb:bindings>
</jaxws:bindings>

上述配置引用了以下项目目录结构:

+-- binding
|   +-- jaxws-binding.xml
+-- wsdl
|   +-- some.wsdl
+-- src
    ...

如果您使用 org.codehaus.mojo:jaxws-maven-plugin 插件,那么您需要指定 <绑定目录>绑定

如果您的 XSD 位于 WSDL 外部,那么您需要单独指定自定义:

+-- binding
|   +-- jaxb-binding.xml
|   +-- jaxws-binding.xml
+-- wsdl
    ...

那么 jaxb-binding.xml 将会如下所示:

<jaxb:bindings version="1.0"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <jaxb:bindings schemaLocation="my.xsd" node="//xsd:schema">
        <jaxb:schemaBindings>
            <jaxb:package name="org.company.project.beans" />
        </jaxb:schemaBindings>
    </jaxb:bindings>
</jaxb:bindings>
  • 对于 Ant 构建,您只需为不同的包生成两个 jar。
  • 由于我个人不知道如何从一个 Maven 项目创建两个 JAR 工件 :) 那么最简单的解决方案是您在 project-beans 项目和 中从 XSD 生成 JAXB 类>project-ws 项目在 wsimport 运行后删除生成的 JAXB 类(您可以使用 ant 插件)。

You can achieve this using JAXB/JAX-WS customization. Suppose you have XSD types embedded into WSDL. Then your customization will look like:

<jaxws:bindings version="2.0"
    xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    wsdlLocation="../wsdl/some.wsdl">

    <jaxws:package name="org.company.project.ws" />

    <!-- XSD types customization within WSDL -->
    <jaxb:bindings node="//xsd:schema">
        <jaxb:schemaBindings>
            <jaxb:package name="org.company.project.beans" />
        </jaxb:schemaBindings>
    </jaxb:bindings>
</jaxws:bindings>

The above configuration refers the following project directory structure:

+-- binding
|   +-- jaxws-binding.xml
+-- wsdl
|   +-- some.wsdl
+-- src
    ...

If you use org.codehaus.mojo:jaxws-maven-plugin plugin, then you need to specify <bindingDirectory>binding</bindingDirectory>.

In case your XSD is external to WSDL, then you need to specify customizations separately:

+-- binding
|   +-- jaxb-binding.xml
|   +-- jaxws-binding.xml
+-- wsdl
    ...

Then jaxb-binding.xml will look like:

<jaxb:bindings version="1.0"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <jaxb:bindings schemaLocation="my.xsd" node="//xsd:schema">
        <jaxb:schemaBindings>
            <jaxb:package name="org.company.project.beans" />
        </jaxb:schemaBindings>
    </jaxb:bindings>
</jaxb:bindings>
  • For Ant build you simply generate two jars for different packages.
  • As I personally do not know any way to create two JAR artefacts from one Maven project :) then the most simple solution would be for you to generate JAXB classes from XSD in project-beans project and in project-ws project remove generated JAXB classes after wsimport run (you can use ant plugin for that).
命硬 2024-12-24 06:02:26

通常我使用 IBM Rational 工具集看到的是:

生成所有 JAXB 和服务类并将它们与服务项目存储在一起。然后重新生成 JAXB 和服务客户​​端类并将它们存储在客户端项目中。

是的,这是重复。但我认为其背后的原因是它分离了服务提供者和服务消费者的关注点。从工具集的角度来看,您如何知道您的客户端是.NET、C++ 还是 Java?或者反之亦然。如果您是客户,您如何知道提供者是.NET、C++ 还是 Java 等?你不知道。因此IBM 提供了这种关注点分离的方式。

现在的缺点是,如果您碰巧同时拥有服务提供者和消费者的源代码,那么您就会有重复的代码。维护起来可能会很痛苦。

因此,也许最好将服务和客户端生成到 Java 项目(而不是 J2EE 项目或 Web 项目)中,并用它制作一个 jar。这样,所有 JAXB 类都在那里(并且仅一次)。 WSDL 就在那里(一次)。该服务只存在一次,并且可以以 EAR 或 WAR 的形式部署到服务器。如果您想将其提供给某人来使用您的服务,则存在客户端。如果您的客户端允许基于 WSDL 位置进行动态创建,那就更好了。

我有 一篇文章可能会从向导驱动的角度为您提供帮助。它与安全性更相关,但您可能会从中找到一些有用的提示。

Usually what I've seen using the IBM Rational toolset:

Generate all the JAXB and service classes and store them with the service project. Then regenerate the JAXB and service client classes and store them in a client project.

Yes, this is duplication. But I think the reasoning behind it is that it separates the concerns of service providers and service consumers. From a toolset perspective, how do you know if your client is .NET, C++, or Java? Or vice versa. If you are a client, how do you know if the provider is .NET, C++, or Java, etc? You don't. Thus IBM provides this way of separation of concerns.

Now the downside to that is that you have duplicate code, if you happen to have the source for both the service provider and the consumer. This can be a pain to maintain.

So perhaps it would be best to generate the service and the client into a Java project (not a J2EE project or a web project) and make a jar out of it. This way, all the JAXB classes are there (and only once). The WSDL is there (once). The service is there once and can be deployed to the server in either an EAR or WAR. And the client exists in case you want to give that to someone to consume your service. If your client allows for dynamic creation based on the WSDL location, even better.

I've got a post that may help you with that from a wizard driven perspective. It is more related to security, but you may find some helpful tips from it.

携余温的黄昏 2024-12-24 06:02:26

如果您使用 Maven,则可以使用插件来执行此操作。
使用 JAXB XJC Maven 2 插件

If you're using maven, you can use a plug-in to do that.
Using the JAXB XJC Maven 2 Plugin

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