当我们使用CXF动态客户端调用WebService接口容易出现如下问题:命名空间问题

Exception in thread "main" org.apache.cxf.common.i18n.UncheckedException: No operation was found with the name {http://impl.service.jws/}sum.
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:289)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:283)
at cxf.bootstrap.CxfDynamicClientOnJwsRtWeb.main(CxfDynamicClientOnJwsRtWeb.java:36)

这个问题是由这个问题因为SIB和SEI类的targetNamespace统一导致的。

解决办法:

SIB的targetNamespace的命名空间为SEI对应的命名空间targetNamespace相同即可。

SEI (interface接口类):

package jws.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService; import entity.User;
/**
* service endpoint interface(SEI)
* RPC接口
* 如果返回结果时实例类,则targetNamespace必须用http://service.jws/,即http://+SEI倒序报名+/
* 没有的话可以,targetNamespace可任意指定http://www.donald.service/jws_service/
* @author donald
* 2017年7月7日
* 下午5:11:53
*/
@WebService(
targetNamespace = "http://service.jws/"
// targetNamespace = "http://www.donald.service/jws_service/"
)
public interface JwsIService {
//@WebMethod注解可写可不写
// @WebMethod
@WebResult(name="sumResult")
public int sum(@WebParam(name="firstNum")int firstNum,
@WebParam(name="secondNum")int secondNum); }

SBI (implements接口实现类):

package jws.service.impl;

import javax.jws.WebService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import entity.User;
import jws.service.JwsIService;
/**
* Service implementation Bean(SIB)
* RPC接口实现
* 注意这里的targetNamespace的命名空间为SEI对应的命名空间,注意最后要加/
* 否则利用CXF动态客户端调用时,会找不到
* Exception in thread "main" org.apache.cxf.common.i18n.UncheckedException:
* No operation was found with the name {http://impl.service.jws/}sum.
* @author donald
* 2017年7月7日
* 下午5:11:49
*/
@WebService(endpointInterface="jws.service.JwsIService",
serviceName = "jwsService",
portName = "jwsPort",
targetNamespace = "http://service.jws/"
// targetNamespace = "http://www.donald.service/jws_service/"
)
public class JwsServiceImpl implements JwsIService {
private static final Logger log = LoggerFactory.getLogger(JwsServiceImpl.class);
@Override
public int sum(int firstNum, int secondNum) {
int result = firstNum+secondNum;
log.info("======"+firstNum+"+"+secondNum+"="+result);
return result;
}
}

动态客户端:

package cxf.bootstrap;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import jws.service.User;
import util.JsonUtil; /**
* CXF 动态代理模式,不用生成本地WS代理类,
* 通过反射调用 WS 的对应的方法,传入相应的参数
* 访问cxf-server-web项目下的webservice;
* 测试jaxws-rt发布WebService web方式。
* 此测试实例,用于测试SEI和SIB的targetNamespace指定的webService接口:
* http://localhost:8080/cxf_server_web/jws_services?wsdl;
* @author donald
* 2017年7月8日
* 下午7:24:12
*/
public class CxfDynamicClientOnJwsRtWeb {
private static final Logger log = LoggerFactory.getLogger(CxfClient.class);
private static final String JWS_RT_WSDL_URI = "http://localhost:8080/cxf_server_web/jws_services?wsdl";
public static void main(String[] args) throws Exception {
log.info("======CXF-WS Dynamic Client start!======");
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(JWS_RT_WSDL_URI);
HTTPConduit conduit = (HTTPConduit)client.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(10000);
policy.setAllowChunking(false);
policy.setReceiveTimeout(10000);
conduit.setClient(policy);
Object[] invokeResult = client.invoke("sum", 17,8);
log.info("=======sumResult:" + invokeResult[0]);
}
}

文章转载至:https://blog.csdn.net/Donald_Draper/article/details/88307382

最新文章

  1. JAVA之IO流(字符流)
  2. jquery 给input赋值错误写法
  3. 在网页中插入MSN,Skype,QQ的方法
  4. Java魔法堂:以Windows服务的形式运行Java程序
  5. Ajax、Comet与Websocket
  6. MySQL5.6 windows7下安装及基本操作
  7. node.js url模块
  8. 在eclipse上安装 Marketplace Client
  9. xml它解析----DOM解析
  10. Linux Ubuntu从零开始部署web环境及项目 -----tomcat+jdk+mysql (二)
  11. Java中增强for循环的用法
  12. Python逻辑运算符
  13. 畅通工程 HDU - 1232
  14. double 四舍五入保留一定的位数
  15. 【LeetCode】108. Convert Sorted Array to Binary Search Tree
  16. 【Android】Android 手机忘记锁屏密码的解决办法
  17. MATLAB System Generator初识
  18. 3、Docker容器管理
  19. Nginx 的动态 upstreams
  20. Java 简明教程

热门文章

  1. 855 gpu强 730 3倍
  2. Linux系统函数read()/write()/pread()/pwrite()的区别-(转自CSDN网络)
  3. linux各文件夹的作用-(转自玉米疯收)
  4. mysql的日志文件及其作用
  5. 5分钟安装docker教程
  6. Google I/O 2021 Android精华内容
  7. 爱心跳动效果 CSS实现
  8. 大数据开发-Flink-窗口全解析
  9. Python小白的数学建模课-A1.2021年数维杯C题(运动会优化比赛模式探索)探讨
  10. JS实现前台表格排序功能