文章目录
- 1. 现象
- 2. 解决办法1
- 3. 解决办法2
1. 现象
整合spring+cxf的webservice,成功发布了wsdl,但在调用的时候报错
org.apache.cxf.common.i18n.UncheckedException: No operation was found with;
2. 解决办法1
: 在service接口中添加targetNamespace
package com.gblfy.service;import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;@WebService(targetNamespace = "http://impl.service.gblfy.com/")
public interface IUserService {@WebMethodpublic String getCxf(@WebParam(name = "reqXml") String reqXml);
}
实现类
package com.gblfy.service.impl;import com.gblfy.service.IUserService;import javax.jws.WebService;@WebService
public class UserServiceImpl implements IUserService {@Overridepublic String getCxf(String reqXml) {System.out.println("接收到的报文:" + reqXml);return "OK";}
}
客户端
/*** 单/多参调用工具类(Object类型)** @param cxfUrl url地址* @param method 调用方法名* @param reqXml 发送报文体* @return res 返回结果* @throws Exception 若有异常,在控制台输出异常,并将异常抛出*/public static String cxfClientParam(String cxfUrl, String method, Object... reqXml) throws Exception {String res = null;// 创建动态客户端JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();Client client = dcf.createClient(cxfUrl);// 需要密码的情况需要加上用户名和密码// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));Object[] objects = new Object[0];try {// 基本格式:invoke("方法名",参数1,参数2,参数3....);objects = client.invoke(method, reqXml);res = objects[0].toString();System.out.println("返回数据:" + res);} catch (java.lang.Exception e) {e.printStackTrace();throw e;}return res;}
3. 解决办法2
使用 QName 然后添加 service接口的地址
package com.gblfy.service.client;import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Component;import javax.xml.namespace.QName;/*** cxf客户端调用(企业内部已封装)** @author gblfy* @date 2021-09-17*/
@Component
public class CxfClient {public static void main(String[] args) throws Exception {String cxfUrl = "http://127.0.0.1:8080/spring_cxf_war/webservice/userWS?wsdl";String method = "getCxf";String reqXml = "cxf请求报文";//调用服务CxfClient.cxfClientParam(cxfUrl, method, reqXml);}/*** 单/多参调用工具类(Object类型)** @param cxfUrl url地址* @param method 调用方法名* @param reqXml 发送报文体* @return res 返回结果* @throws Exception 若有异常,在控制台输出异常,并将异常抛出*/public static String cxfClientParam(String cxfUrl, String method, String reqXml) throws Exception {String res = null;// 创建动态客户端JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();Client client = dcf.createClient(cxfUrl);// 需要密码的情况需要加上用户名和密码// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));Object[] objects = new Object[0];try {// 基本格式:invoke("方法名",参数1,参数2,参数3....);QName qName = new QName("http://impl.service.gblfy.com/",method);objects = client.invoke(qName, reqXml);res = objects[0].toString();System.out.println("返回数据:" + res);} catch (Exception e) {e.printStackTrace();throw e;}return res;}
}