cxf+spring发布webservice和调用

项目所需jar包:http://url.cn/V6RvIw 因为我用到了httpclient,所以里面包含了httpclient的jar包。

建项目什么就不说了。

直接上代码说接口:

写一个demo接口

package  cn.cxf.demo;
 
import  javax.jws.WebService;
 
@WebService
public  interface  Demo {
     String sayHi(String text);
}

然后就需要实现它喽

targetNamespace 是指向接口的包路径

?
package  cn.cxf.demo.impl;
 
import  javax.jws.WebService;
 
import  cn.cxf.demo.Demo;
@WebService (endpointInterface= "cn.cxf.demo.Demo" , serviceName= "DemoService" , targetNamespace= "http://demo.cxf.cn/" )
public  class  DemoImpl  implements  Demo {
 
     @Override
     public  String sayHi(String text) {
         return  "Hi ! "  + text;
     }
 
}

在src目录下添加applicationContext.xml

<? 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:jaxws = "http://cxf.apache.org/jaxws"
     xsi:schemaLocation="
  http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans.xsd 
  http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
  
     < 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"  />
     
     <!-- 测试接口    -->
     < jaxws:endpoint  id = "demoService"
         implementor = "cn.cxf.demo.impl.DemoImpl" 
         address = "/DemoService"  />
     
</ beans >
再修改下web.xml文件就搞定了~(在web.xml添加如下内容就可以了)

< context-param >
     < param-name >contextConfigLocation</ param-name >
     < param-value >classpath:applicationContext.xml</ param-value >
   </ context-param >
   < listener >
     < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class >
   </ listener >
   < servlet >
     < servlet-name >CXFServlet</ servlet-name >
     < servlet-class >org.apache.cxf.transport.servlet.CXFServlet</ servlet-class >
     < load-on-startup >1</ load-on-startup >
   </ servlet >
   < servlet-mapping >
     < servlet-name >CXFServlet</ servlet-name >
     < url-pattern >/ws/*</ url-pattern >
   </ servlet-mapping >

发布一下,访问 http://localhost:8080/cxfdemo/ws 就可以看到接口已经发布成功了。

调用接口就更简单了

如果只是单纯的调用接口的话只需要4个jar包就可以了:

cxf-2.4.3.jar

neethi-3.0.1.jar

wsdl4j-1.6.2.jar

xmlschema-core-2.0.1.jar

可以从上面的jar包里挑选。

调用代码如下:



import  org.apache.cxf.endpoint.Client;
import  org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
 
public  class  CXFWSTest {
     public  static  void  main(String[] args)  throws  Exception {
         JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
         Client client = factory.createClient( "http://localhost:8080/cxfdemo/ws/DemoService?wsdl" );
         Object[] objs = client.invoke( "sayHi" "阿福" );
         System.out.println(objs[ 0 ].toString());
     }
}

import  org.apache.cxf.endpoint.Client;
import  org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
 
public  class  CXFWSTest {
     public  static  void  main(String[] args)  throws  Exception {
         JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
         Client client = factory.createClient( "http://localhost:8080/cxfdemo/ws/DemoService?wsdl" );
         Object[] objs = client.invoke( "sayHi" "阿福" );
         System.out.println(objs[ 0 ].toString());
     }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值