在 CXF wsdl2java 中设置 Java 合规性级别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5654534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:06:15  来源:igfitidea点击:

Set Java Compliance Level in CXF wsdl2java

javacxfwsdl2java

提问by sdoca

I'm brand new to CXF and am trying to create a client from WSDL. I have used Metro and Axis in the past. I downloaded apache-cxf-2.3.3 and used wsdl2java to generate the client stubs. I use Maven and set it up my pom with this:

我是 CXF 的新手,正在尝试从 WSDL 创建客户端。我过去使用过 Metro 和 Axis。我下载了 apache-cxf-2.3.3 并使用 wsdl2java 生成客户端存根。我使用 Maven 并用这个设置我的 pom:

<properties>
    <cxf.version>2.3.3</cxf.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-ws-security</artifactId>
        <version>${cxf.version}</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
                <optimize>true</optimize>
                <debug>true</debug>
            </configuration>
        </plugin>
    </plugins>
</build>

When I build the project, I get these errors:

当我构建项目时,我收到以下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project client-cxf: Compilation failure: Compilation failure:
[ERROR] \Devel\Projects\Client-CXF\src\main\java\my\webservice\ServiceRuntimeException.java:[38,149] cannot find symbol
[ERROR] symbol  : method required()

and

[ERROR] \Devel\Projects\Client-CXF\src\main\java\my\snmpv2\MyService.java:[76,8] cannot find symbol
[ERROR] symbol  : constructor Service(java.net.URL,javax.xml.namespace.QName,javax.xml.ws.WebServiceFeature[])
[ERROR] location: class javax.xml.ws.Service

It appears that the problems are related to the fact that the generated code uses Java 6 features (“require” element for XmlElementRef, new constructors for Service) yet the CXF Maven dependencies are for Java 5.

问题似乎与生成的代码使用 Java 6 特性(XmlElementRef 的“require”元素,Service 的新构造函数)有关,但 CXF Maven 依赖项适用于 Java 5。

Is there a way to specify that the generated code should be Java 5 compliant?

有没有办法指定生成的代码应该符合 Java 5?

回答by Daniel Kulp

Actually, the code that CXF's wsdl2java command line tool is compatible with Java 5, it's just likely not compatible for Java 6. The reason is that it generates code that is JAX-WS 2.2 and JAXB 2.2 compliant. However, the versions of those API's included in Java 6 are only 2.1.

实际上,CXF 的 wsdl2java 命令行工具的代码与 Java 5 兼容,它可能不兼容 Java 6。原因是它生成的代码符合 JAX-WS 2.2 和 JAXB 2.2。但是,Java 6 中包含的这些 API 的版本仅为 2.1。

There are a few options:

有几个选项:

1) Easiest is to add "-fe jaxws21" to the wsdl2java command to have it generate jaxws 2.1 compliant code instead of 2.2

1) 最简单的方法是在 wsdl2java 命令中添加“-fe jaxws21”以使其生成符合 jaxws 2.1 的代码而不是 2.2

2) Add the 2.2 api jars to the endorsed directory of your JDK

2) 将 2.2 api jars 添加到 JDK 的认可目录中

3) Configure the compiler plugin in maven to "endorse" the 2.2 jars

3)在maven中配置编译器插件来“背书”2.2 jars

回答by Powerlord

The wsdl2java documentationdoesn't mention any options for specifying the Java version.

WSDL2Java的文档没有提到指定的Java版本的任何选项。

You could try using the Maven cxf-codegen-pluginto generate the client stubs, which should respect the Maven compiler's source and target versions.

您可以尝试使用 Maven cxf-codegen-plugin生成客户端存根,这应该尊重 Maven 编译器的源和目标版本。

If that doesn't work, you could configure Maven to use Java 6 by changing the source and target lines in the pom, like so:

如果这不起作用,您可以通过更改 pom 中的源和目标行来配置 Maven 以使用 Java 6,如下所示:

               <source>1.6</source>
               <target>1.6</target>