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. 【BZOJ】3673: 可持久化并查集 by zky & 3674: 可持久化并查集加强版(可持久化线段树)

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

  2. 【TYVJ】1467 - 通向聚会的道路(spfa+特殊的技巧)

    http://tyvj.cn/Problem_Show.aspx?id=1467 这题我并不是看题解a的.但是确实从题解得到了启发. 一开始我就想到一个正解,设d[i][0]表示i开始走过奇数个点的最 ...

  3. linux下C语言获取微秒级时间

    使用C语言在linux环境下获得微秒级时间 1.数据结构 int gettimeofday(struct timeval*tv, struct timezone *tz); 其参数tv是保存获取时间结 ...

  4. 李洪强-C语言6-控制结构

    C语言流程控制 一.流程控制结构 (1)顺序结构:按书写顺序执行每一条语句. (2)选择结构:对给定的条件进行判断,根据判断结果决定执行哪一段代码. (3)循环结构:在给定条件成立的情况下,反复执行某 ...

  5. java抛出异常是怎么回事?

    一.Java异常的基础知识 异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的.比如说,你的代码少了一个分号,那么运行出来结果是提示是错误java.lang.Error:如 ...

  6. json格式转换成Map的应用

    jsp 1.引用json.js(将json格式转换成字符串) 2. var name = document.getElementById("name").value; var re ...

  7. JavaWEB入门

    首先,我们已知的是Web资源分为:静态web资源(如html 页面):指web页面中供人们浏览的数据始终是不变.常用的静态web开发技术即为html:动态web 资源:指web页面中供人们浏览的数据是 ...

  8. CodeForces 86D Powerful array(莫队+优化)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

  9. 管理Sass项目文件结构

    构建你的结构体系 CSS预处理器的特点之一是可以把你的代码分割成很多个文件,而且不会影响性能.这都要归功于Sass的@import命令,只要在你的开发环境下,你调用不管多少文件,最终将编译出一个CSS ...

  10. jQuery 实现带下拉提示且自动填充的邮箱

    /* ** 本文例子所引用的jQuery版本为 jQuery-1.8.3.min.js ** Author:博客园小dee*/ 本文用 jQuery 实现一个用户输入字符时出现能够提示邮箱后缀名的下拉 ...