You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java 11下用JAX-WS调用SOAP服务遇WSPolicy引用错误求助

问题分析与解决方案

环境与问题概述

  • Java 11环境,使用com.sun.xml.ws:jaxws-maven-plugin:2.3.2生成SOAP服务调用类
  • pom.xml依赖存在Jakarta API与Java EE版本不匹配问题,触发SAAJ加载机制警告
  • 核心报错:WSP1005: Failed to find policy referenced by URI "http://int-vwe1c01.m2sc.local:8080/EngageOneWS/DeliveryService?wsdl#WSPolicy",JAX-WS无法正确处理WSDL中#开头的Policy引用,Apache CXF可正常运行

分步解决

1. 修复SAAJ警告与依赖版本冲突

当前pom中jakarta.xml.ws-api:3.0.0(Jakarta EE 9,jakarta.*命名空间)与jaxws-ri:2.3.1(Java EE 8,javax.*命名空间)版本不兼容,导致SAAJ警告。需统一为Jakarta EE 9兼容版本:

修改pom.xml依赖:

<dependencies>
    <dependency>
        <groupId>jakarta.xml.ws</groupId>
        <artifactId>jakarta.xml.ws-api</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>jaxws-ri</artifactId>
        <version>3.0.2</version>
        <type>pom</type>
    </dependency>
    <!-- 添加兼容Jakarta的SAAJ实现 -->
    <dependency>
        <groupId>com.sun.xml.messaging.saaj</groupId>
        <artifactId>saaj-impl</artifactId>
        <version>3.0.2</version>
    </dependency>
</dependencies>

同步更新jaxws-maven-plugin版本:

<build>
    <plugins>
        <plugin>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <version>3.0.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>wsimport</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2. 解决WSPolicy引用找不到的问题

方案一:本地托管WSDL与Policy文件

将远程WSDL和ws-policy.xml下载到本地项目(如src/main/resources/wsdl目录),修改主WSDL的导入路径:

<!-- 修改主WSDL中的import标签 -->
<wsdl:import location="ws-policy.xml" namespace="http://pb.com/EngageOne/ws/WSPolicy"/>

更新插件配置指向本地WSDL:

<configuration>
    <wsdlUrls>
        <wsdlUrl>${project.basedir}/src/main/resources/wsdl/DeliveryService.wsdl</wsdlUrl>
    </wsdlUrls>
</configuration>

本地文件加载时,JAX-WS可正确解析#开头的本地引用。

方案二:忽略WS-Policy验证(非生产环境可选)

若服务端不强制要求Policy验证,可通过JVM参数或代码跳过Policy检查:

  • JVM启动参数:
-Dcom.sun.xml.ws.policy.ignore=true
  • 代码中配置:
DeliveryService service = new DeliveryService();
DeliveryServicePortType port = service.getDeliveryServiceSOAPPort();

BindingProvider bp = (BindingProvider) port;
bp.getRequestContext().put("com.sun.xml.ws.policy.ignore", true);

方案三:使用Catalog文件映射远程资源

创建src/main/resources/catalog.xml,将远程Policy文件映射到本地:

<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
    <system 
        systemId="http://int-vwe1c01.m2sc.local:8080/EngageOneWS/DeliveryService?wsdl=ws-policy.xml"
        uri="wsdl/ws-policy.xml"/>
</catalog>

在插件配置中指定catalog文件:

<configuration>
    <wsdlUrls>
        <wsdlUrl>http://int-vwe1c01.m2sc.local:8080/EngageOneWS/DeliveryService?wsdl</wsdlUrl>
    </wsdlUrls>
    <catalog>${project.basedir}/src/main/resources/catalog.xml</catalog>
</configuration>

插件加载远程WSDL时,会自动将指定的远程资源替换为本地文件,解决引用问题。

内容的提问来源于stack exchange,提问作者Stefano Riffaldi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 01:25:24