cxf发布服务,调用服务的博客很多,这里也就简单贴一下代代码。

环境如下:spring+cxf (maven环境)

<cxf.version>2.7.11</cxf.version>

<!-- SOAP begin -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>${cxf.version}</version>
<exclusions>
<!-- use javax.mail.mail instead -->
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-javamail_1.4_spec</artifactId>
</exclusion>
<!-- use javax.activation.activation instead -->
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-activation_1.1_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
<exclusions>
<!-- see above -->
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-javamail_1.4_spec</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-activation_1.1_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- SOAP end -->

服务端(jetty环境):

@WebService
public interface ICommand { /**
* 通用调用
* @param fromApp
* @return
*/
public String isConnection(@WebParam(name="fromApp") String fromApp); public String sendCommand(@WebParam(name="fromApp")String fromApp, @WebParam(name="command")String command); }
@WebService
public class CommandImpl implements ICommand { private final static Logger logger = LoggerFactory.getLogger(CommandImpl.class); @Override
public String isConnection(String fromApp) {
logger.info("from:" + fromApp);
return "ok";
} @Override
public String sendCommand(String fromApp, String command) {
logger.info("from:" + fromApp + ",name:" + command);
// 调用自己生
return "response:[" + command + "]";
}
}

spring配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!--CXF配置-->
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <!--服务端发布的webservice 和下面的配置一样
<jaxws:endpoint id="command" implementor="cn.edu.bupt.service.soa.CommandImpl" address="/Command"/>
-->
<bean id="command" class="cn.edu.bupt.service.soa.CommandImpl"/>
<jaxws:endpoint id="commandWebService" address="/Command" implementorClass="cn.edu.bupt.service.soa.CommandImpl">
<jaxws:implementor ref="command"/>
</jaxws:endpoint>
</beans>

客户端配置:

 <jaxws:client id="commandClient" address="http://localhost:8088/virtual-network/webservice/Command" serviceClass="cn.edu.bupt.service.soa.ICommand"/>
    

采用bean调用:

public static void main(String[] args){

   ApplicationContext context = new ClassPathXmlApplicationContext("spring-cxf.xml");
ICommand helloWorld=(ICommand)context.getBean("commandClient");
System.out.println(helloWorld.sendCommand("helloworld","Test"));
}

这边准备用cxf动态调用:

 public static void main(String[] args){
JaxWsDynamicClientFactory factory =JaxWsDynamicClientFactory.newInstance();
String url = "http://localhost:8088/virtual-network/webservice/Command?wsdl";
org.apache.cxf.endpoint.Client client =factory.createClient(url);
//url为调用webService的wsdl地址
//QName也可以
//QName name=new QName("http://soa.service.bupt.edu.cn/","isConnection");
try {
// Object[] obj =client.invoke(name, "helloworld");
Object[] obj =client.invoke("isConnection", "helloworld");
System.out.println("resp:"+obj[0]);
} catch (Exception e) {
e.printStackTrace();
}
}

由于我客户端和服务端是一起运行,报出异常:

Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.sun.tools.internal.xjc.reader.Ring.get(Ring.java:87)
... 23 more
Caused by: java.lang.NoSuchFieldError: REFLECTION
at com.sun.tools.internal.xjc.model.nav.NavigatorImpl.getBaseClass(NavigatorImpl.java:59)
at com.sun.tools.internal.xjc.model.nav.NavigatorImpl.getBaseClass(NavigatorImpl.java:44)
at com.sun.xml.internal.bind.v2.model.core.Adapter.<init>(Adapter.java:73)

这个是为什么,因为项目引入的tools.jar与jaxb-impl.jar冲突了。

用mvn dependency:tree > tree.txt 导入包引用路径,发现是druid引入的tools导致的

[INFO] +- com.alibaba:druid:jar:1.0.9:compile
[INFO] | +- com.alibaba:jconsole:jar:1.8.0:system
[INFO] | \- com.alibaba:tools:jar:1.8.0:system

排除一下

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>jconsole</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>tools</artifactId>
</exclusion>
</exclusions>
</dependency>

就解决了该冲突。

参考:

http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html

http://jishiweili.iteye.com/blog/2086100

http://www.blogjava.net/sxyx2008/archive/2010/09/15/332058.html

http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html

cxf动态调用wsdl的一个冲突以及解决的更多相关文章

  1. CXF动态调用wsdl接口

    1.application.properties文件中配置接口url 2.工具类 package com.vulnverify.core.utils; import java.io.IOExcepti ...

  2. cxf动态调用webservice设置超时,测试线程安全

    Java代码 import java.util.Random; import java.util.concurrent.ArrayBlockingQueue; import java.util.con ...

  3. 转载 CXF动态调用webservice

    /** * * @param wsdlUrl wsdl的地址:http://localhost:8001/demo/HelloServiceDemoUrl?wsdl * @param methodNa ...

  4. cxf动态调用外部web service 报告异常java.lang.NoSuchFieldErr

    原因:cxf 依赖的xmlschema-core 与axis2-kernel依赖的xmlschema冲突. 解决方法:因为在项目中只用cxf即可,所以删除axis2的依赖.

  5. cxf动态调用外部web service 报告异常java.lang.NoSuchFieldError: QUALIFIED

    原因:cxf 依赖的xmlschema-core 与xfire-all依赖的xmlschema冲突.(百度搜索亦得知:cxf 依赖的xmlschema-core 与axis2-kernel依赖的xml ...

  6. cxf 动态调用.

    import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache ...

  7. H5 input输入限制最大位数,和调用小键盘需求发生冲突的解决办法

    首先,限制输入最大位数时,input有自带的属性maxlength. <input type="text" name="email" maxlength= ...

  8. .net WebServer示例及调用(接口WSDL动态调用 JAVA)

    新建.asmx页面 using System; using System.Collections.Generic; using System.Linq; using System.Web; using ...

  9. C#动态调用带有SoapHeader验证的WebServices

    http://blog.csdn.net/u012995964/article/details/54573143 本文记录C#中通过反射动态的调用带有SoapHeader验证的WebServices服 ...

随机推荐

  1. TYVJ P1088 treat Label:鞭笞人的DP

    时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 广东汕头聿怀初中 Train#2 Problem2 描述 给出长度为N的数列{A_i},每次可以从最左边或者最 ...

  2. 【BZOJ】1862: [Zjoi2006]GameZ游戏排名系统 & 1056: [HAOI2008]排名系统(treap+非常小心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1862 http://www.lydsy.com/JudgeOnline/problem.php?id ...

  3. Struts1与Struts2的12点区别

    Struts1与Struts2的12点区别  1) 在Action实现类方面的对比:Struts 1要求Action类继承一个抽象基类:Struts 1的一个具体问题是使用抽象类编程而不是接口.Str ...

  4. https的了解

    经常用支付宝,看到了https就查了一下. HTTPS(Secure Hypertext Transfer Protocol)安全超文本传输协议. 简单讲是HTTP的安全版.即HTTP下加入SSL层, ...

  5. 直接双击运行PowerShell的脚本文件

    原来的不支持有空格的路径,由dugu的批处理中找到方法了,利用windows路径的另外的另外表达方式即可,".\路径" 这个东西用的不多啊,使用这个格式后powershell就能不 ...

  6. pod 安装 Masonry 遇到问题

    pod 导入第三方库 Masonry: 在工程masonryTest的文件下新建一个Podfile文件 编辑如下内容: platform :ios, '8.0'xcodeproj 'mansoryTe ...

  7. html5_d登陆界面_注册界面

    <!DOCTYPE html><html><head><script type="text/javascript">function ...

  8. 【转】在.Net中关于AOP的实现

    原文地址:http://www.uml.org.cn/net/201004213.asp 一.AOP实现初步 AOP将软件系统分为两个部分:核心关注点和横切关注点.核心关注点更多的是Domain Lo ...

  9. 如何用Apache POI操作Excel文件-----如何用Apache POI 画一个离散图

    有的时候,我们需要Excel中的数据,通过一个图画,可视化的表现出来. 那么这个时候,应该如何做呢?现在就借花献佛,以Apache POI自己提供的一个例子为例,给大家演示一下POI的API 如何画图 ...

  10. 使用Eclipse开发Java Web过程中Debug调试的使用方法

    里介绍的是在Eclipse中的Debug调试. 首先右击项目选择Debug As -- Debug on Server 或者点击Server面板的小昆虫图标,启动Debug模式. 运行web项目,进行 ...