您的位置:首页 > 运维架构 > Apache

使用apache-cxf-2.2.10来制作一个极简版WebService程序

2013-11-21 22:08 537 查看
原想拿最新版cxf来制作的,无奈Apache的zip包总下不下来,国内的apache-cxf-2.2.10却一蹴而就,也就用了这个版本。下载地址是:http://pan.baidu.com/s/1tdgiW

整个程序的下载地址是:http://pan.baidu.com/s/1GdI5p ,里面包括服务器端和测试客户端,伸手党直接拿走就好了。

用cxf做Webservice还是很顺手的,感觉比以前的Axis1/2都要简单快捷。

首先,讲讲如何做服务器端。

先用MyEclipse创建一个Web工程,把apache-cxf-2.2.10里面lib目录的jar包都拷到web-inf下的lib目录里,再将所有lib引导到工程中。

其次,把Web.xml改写如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Rts Cxf WebService</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/appctx.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>/*</url-pattern>
</servlet-mapping>
</web-app>


然后,在WEB-INF下创建一个appctx.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" />

<!-- 上面不用管 ,id随意,implementor是实现类,address在客户端的地址中会用到-->
<jaxws:endpoint id="getresponse" implementor="com.hy.TestImpl" address="/getResponse" />
</beans>


然后,创建一个接口:

package com.hy;

import javax.jws.WebService;

@WebService
public interface Test{
public String getResponse(String req);
}


在实现一个类:

package com.hy;

import javax.jws.WebService;

@WebService
public class TestImpl implements Test{

public String getResponse(String req) {

return "Hello!"+req;
}
}


注意上面的Annotator “@WebService” 不能少,我都用粗体标识出来了,少了就玩不转了。

之后,把工程发布到Tomcat里,WebService就运转起来了。

在浏览器地址栏输入http://localhost:8080/cxfServer是这个效果:



服务端运转起来,需要客户端来测试一下。

这回是制作一个java工程,将lib同样倒一下。

然后把Test接口拷贝过来,再写一个测试程序就好了:

package com.hy;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class Main{
public static void main(String[] args){
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(Test.class);
factory.setAddress("http://localhost:8080/cxfServer/getResponse");  // localhost:8080/ + appname+/+address(appctx.xml里写的)
Test t = (Test) factory.create();
System.out.println(t.getResponse("逆火狂飙"));
}
}


这个跑完的效果是:Hello!逆火狂飙

就是这些内容,确实简单。你可以以此为基础发明自己的WebService。

2013年11月21日22:06:13
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: