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. JSON.stringify语法解析(自己留存)

    语法: JSON.stringify(value [, replacer] [, space]) value:是必选字段.就是你输入的对象,比如数组,类等. replacer:这个是可选的.它又分为2 ...

  2. tableviewCell折叠状态2

    // //  LHQContentViewCell.h //  11 - 投资管理 - 李洪强 // //  Created by vic fan on 16/4/12. //  Copyright ...

  3. 一、saltstack简介和安装

    系统环境:CentOS6.5 准备yum源: epel源(包含了saltstack的包).阿里源(CentOS-Base.repo) Host解析文件: # cat /etc/hosts 192.16 ...

  4. Windows系统中Git的安装配置

    一.Git安装 1.下载 Git官网:https://git-scm.com/download/ 选择windows版本下载即可. 百度软件中心:http://rj.baidu.com/ 如官网下载不 ...

  5. 小红伞和virtualbox5.0.10冲突

    win7 sp1 64bit 旗舰版:virtual box 5.0.10 提示 error in supr3hardNtChildWaitFor……Timed out after 60001 ms ...

  6. A trip through the Graphics Pipeline 2011_01

    It’s been awhile since I posted something here, and I figured I might use this spot to explain some ...

  7. 投芯片,现在要n+1模式

    给坚持理想的屌丝点个赞.投芯片,现在要n+1模式,n个小项目+一个大项目.团队是基础,屌丝创业要以营利为先导,先要短快,同时要有并行的大项目支撑.小项目赚的钱先解决面包问题,同时为大项目锻炼队伍积累经 ...

  8. PHP 设计模式 笔记与总结(7)适配器模式

    ① 适配器模式可以将截然不同的函数接口封装成统一的 API ② 实际应用举例:PHP 的数据库操作有 mysql,mysqli,pdo 三种,可以用适配器模式统一成一致.类似的场景还有 cache 适 ...

  9. 将Asset中的数据库文件拷贝出来使用

    设置保存路径 private final static String DATABASE_PATH = "/data"+ Environment.getDataDirectory() ...

  10. subclipse安装后从svn资源库视图check out的资源无法创建server

    不要从根目录下check out,只要把根目录下所需要的项目check out即可.