Apache CXF Webservice入门

1.步骤一览

关于CXF的介绍请移步官网百科,这里仅供初次使用者入门。

2.步骤详情

2.1.环境准备

apache-cxf-3.0.0.zip下载

jdk1.7.0_51

Eclipse4.3.0

Tomcat v7.0 Server

2.2.创建服务端

2.2.1.新建Dynamic web project如下

image

默认的eclipse编译输出目录:buildclasses,这里就不作修改了,如下:

image

生成web.xml选项还是选上吧,免的自己还要创建文件,默认生成的主要就是welcome-file-list,其实也没太多用处

image

image

2.2.2.引入cxf相应jar文件

将下载的apache-cxf-3.0.0.zip解压,目录结构如下

image

将lib文件夹下的所有文件拷贝到工程中(有些非核心jar是不需要的,根据实际工程删减),项目结构如下:

image

2.2.3.创建服务接口

image

ICxfTest内容如下:

package com.ibugs.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface ICxfTest {

    @WebMethod
    String hi(@WebParam(name="name") String name);
}

与一般接口不同,在ICxfTest接口中引入了三个注解:@WebService,@WebMethod,@WebParam

@WebService 标记在接口上表示此接口为WebService服务

@WebMethod 表示为WebService方法

@WebParam 则表示参数重命名

这样一个WebService接口就定义好了

2.2.4.接口实现

image

代码和一般实现没什么区别,如下

package com.ibugs.service;

public class CxfTest implements ICxfTest {

    @Override
    public String hi(String name) {
        
        return "hi " + (name != null?  name : "Everyone" );
    }

}

2.2.5.配置servlet

修改web.xml文件,增加servlet

<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>/webservice/*</url-pattern>
  </servlet-mapping>

2.2.6.cxf-servlet.xml

解压目录apache-cxf-3.0.0sampleswsdl_firstsrcmainwebappWEB-INF中复制文件cxf-servlet.xml到/CxfService/WebContent/WEB-INF

修改代码:

<?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" xmlns:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
    <bean id="Hi" class="com.ibugs.service.CxfTest"/>
    <jaxws:endpoint id="hi" implementor="#Hi" address="/hi"/>
</beans>

主要是配置jaxws:endpoint,<jaxws:endpoint>定义了一个webservice,implementor是webservice的处理类,值定义在<bean>中指定的id,具体实现在class属性中配置;address是访问路径

2.2.7.tomcat发布

发布过程与一般web工程没有任何区别,访问路径localhost:8080/CxfService/webservice

image

点击相应的链接:

image

至此服务端部署完成

2.3.客户端

2.3.1.生成存根stub

CXF提供 了wsdl2java.bat工具,在解压目录apache-cxf-3.0.0in下,为了使用方便可以将其加入到计算机环境变量Path中

image

相应的参数如上,这里将生成的代码放到D:stub目录下,最后一个参数则是服务端提供的超链接

image

可以用参数-p自己定义包名,默认和服务器所在包名相同,生成的代码D:stubcomibugsservice如下:

image

2.3.2.客户端项目

新建项目:com.ibugs.service包下放自动生成文件,

image

调用webservice,新建ClientTest类代码如下:

package com.ibugs.client;

import com.ibugs.service.CxfTestService;
import com.ibugs.service.ICxfTest;

public class ClientTest {
    static CxfTestService cxfTestService = new CxfTestService();
    static ICxfTest cxfTestPort = cxfTestService.getCxfTestPort();
    
    public static void main(String[] args){
        System.out.println(cxfTestPort.hi("ibugs"));
    }
}

其中cxfTestService为客户端下webservice服务的一个view,而cxfTestPort为其中的一个Proxy。调用的时候通过代理cxfTestPort就可以调用webService的方法cxfTestPort.hi("ibugs")了。这里大家也可以通过Spring依赖注入的方式创建对象就不在此展开了

原文地址:https://www.cnblogs.com/i-bugs/p/3755615.html