使用CXF创建webservice非常简单,使用步骤如下:
服务端
1、创建接口
@WebService public interface HelloService { public @WebResult(name="msg")String sayHello(@WebParam(name="name")String name); }
2、创建接口的实现类
@WebService(endpointInterface="com.luo.service.HelloService",serviceName="helloService") public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { System.out.println("你好"+name); return "你好"+name; } }
3、为了将接口发布出去,在接口和实现类上加上@webservice注解
4、创建服务端发布webservice的主方法:一种是通过CXF内置的Jetty应用服务器发布(见方法一,二),一种是通过tomcat发布(见方法三)
方法一: 最简单,不需要额外的配置,使用sunjax-ws的Endpoint.publish()发布
public class MainServer2 { public static void main(String[] args) { Endpoint endpoint = Endpoint.publish("http://localhost:9000/hello",new HelloServiceImpl()); } }
方法二:用CXF的JaxWsServerFactoryBean类进行发布。(需要CXF相关包)
public class MainServer1 { public static void main(String[] args) { JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean(); factoryBean.setAddress("http://localhost:9000/hello");//设置发布地址 factoryBean.setServiceClass(HelloService.class);//设置接口 factoryBean.setServiceBean(new HelloServiceImpl());//设置接口的实现类 // factoryBean.getInInterceptors().add(new LoggingInInterceptor()); // factoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); factoryBean.create();//发布 } }
方法三:spring结合cxf,通过tomcat发布(后面讲解)
5、然后访问http://localhost:9000/hello?wsdl查看wsdl报文xml
客户端:
6、客户端代码生成:根据wsdl,使用wsdl2java的命令行生成客户端代码
进入dos窗口,进入cxf下的bin目录下,使用:
wsdl2java -p com.luo.client -d E:/ http://localhost:9000/hello?wsdl
具体:
-p 指定其wsdl的命名空间,也就是要生成代码的包名; -d 指定要产生代码所在目录; -client 生成客户端测试web service的代码; -server 生成服务器启动web service的代码; -impl 生成web service的实现代码; -ant 生成build.xml文件;
7、调用web服务
方法一:
使用标准的sunjax-ws的api来完成客户端调用,不需要额外的配置和包
public class MainClient { public static void main(String[] args) throws MalformedURLException { QName qName = new QName("http://service.luo.com/","HelloService_Service"); HelloService_Service helloService_Service = new HelloService_Service(new URL("http://localhost:9000/hello?wsdl"),qName); HelloService helloService = helloService_Service.getPort(HelloService.class); helloService.sayHello("tom"); } }
方法二、用CXF中JaxWsProxyFactoryBean客户端代理工厂调用web服务(需要导入CXF相关包)
public class MainClient2 { public static void main(String[] args){ JaxWsProxyFactoryBean proxyFactoryBean = new JaxWsProxyFactoryBean(); proxyFactoryBean.setAddress("http://localhost:9000/hello"); proxyFactoryBean.setServiceClass(HelloService.class); HelloService helloService = (HelloService) proxyFactoryBean.create(); helloService.sayHello("tom"); } }
方法三:
public class MainClient3 { public static void main(String[] args) { Service service = Service.create(new QName("http://service.luo.com/")); service.addPort(new QName("http://service.luo.com/","HelloServiceImplPort") ,SOAPBinding.SOAP11HTTP_BINDING , "http://localhost:9000/hello"); HelloService helloService = service.getPort(HelloService.class); helloService.sayHello("tom"); } }
方法四:(需要导入CXF相关包)
JaxWsDynamicClientFactory dcf =JaxWsDynamicClientFactory.newInstance(); org.apache.cxf.endpoint.Client client = dcf.createClient("http://127.0.0.1:8080/WSCXF/helloService?wsdl"); //sayHello 为接口中定义的方法名称 张三为传递的参数 返回一个Object数组 Object[] objects=client.invoke("sayHello","张三"); //输出调用结果 System.out.println(objects[0].toString());
8、CXF和Spring整合
8.1 添加jar
8.2 web.xml下添加cxf的servlet:将会过滤所有/services/*的url
<!-- CXF WebService --> <servlet> <servlet-name>CXFServlet</servlet-name> <display-name>CXF Servlet</display-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>3</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
8.3 到application-context.xml中添加3个地方:
<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" />
可以在原有配置文件上修改
8.4 创建webservice服务端的接口和实现类,平时使用的接口和类一样,只是添加了对应的注解
8.5 在applicaiton-context.xml中添加bean
<jaxws:endpoint id="helloworld" implementor="com.luo.HelloworldImpl" address="/helloworld"/>
8.6 编写客户端WebService的接口,这里还使用先前写的Hello.java
8.7 在spring的配置文件添加如下内容:
<jaxws:client id="helloClient" serviceClass="com.luo.IHelloworld" address="http://localhost:8080/cxfSpring/helloworld"/>
8.8 在代码用调用这个bean:
ServletContext sc = getServletContext(); WebApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); Hello hello = (Hello) ac.getBean("helloClient"); String str = hello.say("zhang");
相关推荐
使用CXF开发搭建WebService步骤文档 保证你有收获
CXF WebService整合Spring示例工程代码demo可以直接导入eclipse。参照网页http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html 完成的webService服务提供。 大致步骤: 1.引入cxf和其他需要的jar包,(本...
开发框架请自行搭建 然后再配置里面按照文档中的步骤来实现
CXF开发webservice客户端,详细步骤 希望对大家有所帮助
CXF开发教程,WebService,CXF开发步骤,CXF Demo
傻瓜试记录开发步骤,具体实现自己去完成。 如果看不懂,找自已原因。
SSH CXF webservice 开发笔记demo(包含步骤文档及所需war包)
NULL 博文链接:https://fanshuyao.iteye.com/blog/2246579
说明: 1.本身是一个eclipse 的java project 2.需要JDK5,cxf 2.3.4 log4...关于cxf的深入研究,我发现国人翻译的东西很少,我想尽自己的微薄之力能给大家带些有用的东西,如果有什么关于cxf不明白之处,可以给我留言。
使用cxf开发webservice,是一个简单的demo,有完整的eclipse项目,还有所有涉及到的lib包,还有一份word写的文档,记录了所有的操作步骤。 使用的cxf版本为2.5.2
使用cxf 来实现的webservice,包括客户端代码是书写,wsdl文件的导出,和客户端代码的书写,有截图,关键地方有提示,完全是按照我实现时的步骤一步一步来的,和大家分享一下。
文档包括 webservice 的130多页的ppt 很详细 入门必备 还有个外文文档 很经典 可当随笔 简易项目的必要步骤
webservice 框架的使用步骤,具体可参考,然后就可以实现简单的学习。都是基于idea的开发,比较快捷和灵活
完全是源码,服务的开发及 客户端调用都是源码,发部也说得非常详细,非常适合于初学者,直接照搬照用就可以学会,本人一点一点写的,而且经过了测试,步骤也比较细,截图 图文并茂的形式,大家一定一学就会的.上面留有本人的...
对于测试,请使用 (在Unittest中测试我们的服务将是后续步骤的一部分)。 完全合同优先,使用生成的JAX-WS服务类不包装WSDL并使用原始的WSDL-包括正确的URL和TargetNamespace(推荐) 步骤4:随博客文章一起发布...
五、我们还可以使用Idea下的webservice,能够使用图形画面的方式获取本地代理和生成WSDL文件。 Activiti 介绍 Activiti5是一个业务流程管理(BPM)框架 如果我们的业务是比较复杂的话,我们才会用到...
文件里有CxfWebService 服务端,客户端代码,apache-cxf-2.7.6安装包和cxfWebService开发的所有jar包,还有开发过程的详细步骤,适合初学的朋友,有不足之处还望多多指教