我需要在项目中使用web服务。我使用NetBeans,所以我右键单击我的项目,并尝试添加一个新的“Web服务客户端”。上次我检查时,这是创建web服务客户机的方法。但它导致了一个AssertionError,说:

java.lang.AssertionError: org.xml.sax.SAXParseException;systemId: jar文件:/道路/ / glassfish /模块/ jaxb-osgi.jar ! / com/sun/tools/xjc/reader/xmlschema/bindinfo/binding.xsd;lineNumber: 52个;columnNumber: 88;schema_reference:读取架构文档'xjc失败。由于accessExternalSchema属性设置的限制,不允许访问“文件”。

NetBeans的默认Java平台是JDK8 (Oracle的官方版本),所以当我更改NetBeans .conf文件并将JDK7(也来自Oracle)作为我的默认平台时,一切都工作得很好。所以我认为问题出在JDK8上。这是我的java -version输出:

Java版本“1.8.0” Java(TM) SE运行时环境(build 1.8.0-b132) Java HotSpot(TM) 64位服务器虚拟机(build 25.0-b70,混合模式)

目前,我将JDK7作为默认的Java平台。如果有让JDK8工作的方法,请分享。


当前回答

我在2.4版的artifact org.codehaus.mojo上测试了这个功能,效果很好~

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxws-maven-plugin</artifactId>
        <version>2.4.1</version>
        <executions>
            <execution>

                <goals>
                    <goal>wsimport</goal>
                </goals>
                <configuration>
                    <wsdlDirectory>path/to/dir/wsdl</wsdlDirectory>
                </configuration>
                <id>wsimport-web-service</id>
                <phase>generate-sources</phase>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>javax.xml</groupId>
                <artifactId>webservices-api</artifactId>
                <version>${webservices-api-version}</version>
            </dependency>
        </dependencies>
        <configuration>
            <vmArgs>
                <vmArg>-Djavax.xml.accessExternalSchema=all</vmArg>
            </vmArgs>
            <sourceDestDir>generated-sources/jaxws-wsimport</sourceDestDir>
            <xnocompile>true</xnocompile>
            <verbose>true</verbose>
            <extension>true</extension>
            <sei>/</sei>
        </configuration>
    </plugin>
</plugins>

其他回答

对于没有管理员权限的gradle用户,这里有一个提示:将这一行添加到你的jaxb-task中:

System.setProperty('javax.xml.accessExternalSchema', 'all')

它看起来是这样的:

jaxb {
    System.setProperty('javax.xml.accessExternalSchema', 'all')
    xsdDir = "${project.name}/xsd"
    xjc {
        taskClassname = "com.sun.tools.xjc.XJCTask"
        args = ["-npa", "-no-header"]
    }
}

如果您在使用cxf-codegen-plugin将wsdl转换为jave时遇到了这个问题,那么您可以通过将插件配置为fork并提供额外的“-Djavax.xml”来解决这个问题。accessExternalSchema=all" JVM选项。

        <plugin>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-codegen-plugin</artifactId>
            <version>${cxf.version}</version>
            <executions>
                <execution>
                    <id>generate-sources</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <fork>always</fork>
                        <additionalJvmArgs>
                            -Djavax.xml.accessExternalSchema=all
                        </additionalJvmArgs>

我刚刚尝试过,如果您使用SoapUI (5.4.x)并使用Apache CXF工具生成java代码,请将javax.xml.accessExternalSchema = all放在YOUR_JDK/jre/lib/jaxp中。属性文件也可以工作。

我在Eclipse IDE (4.4, Luna, Windows 7 x64)中运行蚂蚁构建。与其修改已安装的JRE库或任何ant脚本(我有多个项目,它们的构建中包含XJC),我更喜欢更改Eclipse设置“外部工具配置”,并将以下内容添加到ant构建配置的VM参数中:

-Djavax.xml.accessExternalSchema=all

我在一个常规的maven项目中使用了它,并通过运行xjc插件的插件依赖配置解决了这个问题:

     <plugin>
        <!-- Needed to run the plugin xjc en Java 8 or superior -->
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-2</version>
        <executions>
            <execution>
                <id>set-additional-system-properties</id>
                <goals>
                    <goal>set-system-properties</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <properties>
                <property>
                    <name>javax.xml.accessExternalSchema</name>
                    <value>all</value>
                </property>
                <property>
                    <name>javax.xml.accessExternalDTD</name>
                    <value>all</value>
                </property>
            </properties>
        </configuration>
    </plugin>