解决:A SOAP 1.2 message is not valid when sent to a SOAP 1.1 only endpoint.

在使用CXF生成的Web Service客户端调用时遇到'A SOAP 1.2 message is not valid when sent to a SOAP 1.1 only endpoint.'错误。问题源于客户端与服务端SOAP协议版本不一致。通过TCP/IP Monitor工具发现,客户端发送SOAP 1.1,而服务端使用SOAP 1.2。尝试修改wsdl头文件声明无效。深入研究CXF源码发现,默认SOAP版本为1.1,需要在创建接口实例前手动设置为1.2。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用CXF解析wsdl文件生成webservice的客户端后,在调用时可能会爆出这个问题,这是因为客户端调用接口发送的soap协议和服务端接口接受的soap协议不一致所致,在Eclipse或者MyEclipse中,可以使用TCP/IP Monitor 进行监控,查看客户端方和服务端方的soap协议版本。

在我的实际应用中,生成java代码后,调用接口发生了A SOAP 1.2 message is not valid when sent to a SOAP 1.1 only endpoint.错误,监控通信内容如下:


这是我发送出去的,使用的是soap1.1的协议;


这是服务器端返回的信息,由图可知服务端使用的是soap1.2的协议。

我尝试了更改wsdl的头文件声明,再生成的java类还是一样的错误。后来查看cxf源码得知soap的默认版本是soap1.1,需要在生成接口实例前修改soap版本为1.2。

代码如下:

private static JaxWsProxyFactoryBean jobFactory; // WS的代理连接
private static xxWebService xxWebService;
public static xxWebService getxxWebService() {
        try {
            String url = "http://127.0.0.1:8088";
            log.info("getxxWebService Ready");
            if (jobFactory != null) {
                String str = jobFactory.getAddress();
                if ((str != null) && (str.equals(url))) {
                    return xxWebService;
                }
            }
            jobFactory = new JaxWsProxyFactoryBean();
            jobFactory.setServiceClass(xxWebService.class);
            jobFactory.setAddress(url);
            jobFactory.getOutInterceptors().add(new LoggingOutInterceptor());
            //默认为soap1.1,这里改为soap1.2协议发送
            SoapBindingConfiguration config = new SoapBindingConfiguration();
           <strong><em><u> </u>Soap12 sv = Soap12.getInstance();
            config.setVersion(sv);
            jobFactory.setBindingId(SoapBindingConstants.SOAP12_BINDING_ID);
            jobFactory.setBindingConfig(config);</em></strong>
            xxWebService = (xxWebService)jobFactory.create();
            
            Client client = ClientProxy.getClient(xxWebService);
            HTTPConduit conduit = (HTTPConduit)client.getConduit();
            HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
            httpClientPolicy.setConnectionTimeout(10000);
            httpClientPolicy.setReceiveTimeout(30000);
            // 解决Marshalling Error: Error writing request body to server
            httpClientPolicy.setAllowChunking(false);// 取消块编码
            conduit.setClient(httpClientPolicy);
            
            return xxWebService;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

这样就可以以soap1.2协议调用接口了。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值