设为首页 收藏本站
查看: 1708|回复: 0

[经验分享] Apache CXF实现WebService入门教程(附完整源码)

[复制链接]

尚未签到

发表于 2017-2-28 08:42:44 | 显示全部楼层 |阅读模式
  Apache CXF实现WebService非常简单实用,只需要几步就可以实现一个简单的web service。
  首先我们需要新建一个maven项目,在pom中添加依赖和jetty作为测试的web service的web容器。
  如下是测试用到的pom文件内容:



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.outofmemory</groupId>
<artifactId>hello-apache-cxf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hello-apache-cxf</name>
<url>http://maven.apache.org</url>
<properties>  
<cxf.version>2.2.7</cxf.version>  
</properties>  
<dependencies>  
<dependency>  
<groupId>org.apache.cxf</groupId>  
<artifactId>cxf-rt-frontend-jaxws</artifactId>  
<version>${cxf.version}</version>  
</dependency>  
<dependency>  
<groupId>org.apache.cxf</groupId>  
<artifactId>cxf-rt-transports-http</artifactId>  
<version>${cxf.version}</version>  
</dependency>  
<dependency>  
<groupId>org.apache.cxf</groupId>  
<artifactId>cxf-rt-transports-http-jetty</artifactId>  
<version>${cxf.version}</version>  
</dependency>  
<dependency>  
<groupId>org.apache.cxf</groupId>  
<artifactId>cxf-rt-ws-security</artifactId>  
<version>${cxf.version}</version>  
</dependency>  
<dependency>  
<groupId>org.apache.cxf</groupId>  
<artifactId>cxf-rt-ws-policy</artifactId>  
<version>${cxf.version}</version>  
</dependency>  
<dependency>  
<groupId>org.apache.cxf</groupId>  
<artifactId>cxf-bundle-jaxrs</artifactId>  
<version>${cxf.version}</version>  
</dependency>  
<dependency>  
<groupId>javax.ws.rs</groupId>  
<artifactId>jsr311-api</artifactId>  
<version>1.1.1</version>  
</dependency>  
<dependency>  
<groupId>org.slf4j</groupId>  
<artifactId>slf4j-api</artifactId>  
<version>1.5.8</version>  
</dependency>  
<dependency>  
<groupId>org.slf4j</groupId>  
<artifactId>slf4j-jdk14</artifactId>  
<version>1.5.8</version>  
</dependency>  
<dependency>  
<groupId>commons-httpclient</groupId>  
<artifactId>commons-httpclient</artifactId>  
<version>3.0</version>  
</dependency>  
<dependency>  
<groupId>commons-io</groupId>  
<artifactId>commons-io</artifactId>  
<version>2.3</version>  
</dependency>  
<dependency>  
<groupId>junit</groupId>  
<artifactId>junit</artifactId>  
<version>4.8.1</version>  
<scope>test</scope>  
</dependency>  
</dependencies>
<build>  
<finalName>hello-apache-cxf</finalName>  
<resources>  
<resource>  
<directory>src/main/resources</directory>  
</resource>  
<resource>  
<directory>src/main/java</directory>  
<includes>  
<include>**</include>  
</includes>  
<excludes>  
<exclude>**/*.java</exclude>  
</excludes>  
</resource>  
</resources>  
<plugins>  
<plugin>  
<groupId>org.mortbay.jetty</groupId>  
<artifactId>maven-jetty-plugin</artifactId>  
<configuration>  
<contextPath>/</contextPath>  
<connectors>  
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">  
<port>9000</port>  
</connector>  
</connectors>  
</configuration>  
</plugin>  
<plugin>  
<groupId>org.apache.maven.plugins</groupId>  
<artifactId>maven-compiler-plugin</artifactId>  
<configuration>  
<source>1.5</source>  
<target>1.5</target>  
</configuration>  
</plugin>  
</plugins>  
</build>  
</project>
  然后需要定义web service接口,在接口定义中要添加必要的annotation注解来标注出来webservice接口和提供的方法,以及参数等,如下接口文件:



package cn.outofmemory.hello.apache.cxf;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService
public interface HelloService {
@WebMethod
@WebResult String hello(@WebParam String who);
}
  定义完接口之后需要实现接口,接口实现代码如下:



package cn.outofmemory.hello.apache.cxf;
public class SimpleHelloService implements HelloService {
public String hello(String who) {
return "hello " + who;
}
}
  这个实现类不需要做任何的标注。
  这样webservice的实现部分就算完了,我们需要在web容器中运行web service,如下Server代码:



package cn.outofmemory.hello.apache.cxf;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class Server {
public static void main(String[] args) throws Exception {
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();  
factory.setServiceClass(SimpleHelloService.class);  
factory.setAddress("http://localhost:9000/ws/HelloService");  
factory.create();  
System.out.println("Server start...");  
}
}
  可以运行这个类,然后在浏览器中访问:http://localhost:9000/ws/HelloService。
  可以让Server端保持启动状态,下面我们写Client端来调用server端的webservice,如下client端代码:



package cn.outofmemory.hello.apache.cxf;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class ServiceClient {
public static void main(String[] args) {  
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
factory.setServiceClass(HelloService.class);  
factory.setAddress("http://localhost:9000/ws/HelloService");  
HelloService helloworld = (HelloService) factory.create();  
System.out.println(helloworld.hello("outofmemory.cn"));  
System.exit(0);  
}  
}
  运行client,可以得到hello outofmemory.cn的输出。
  下载本文源代码
  相关:
  WebService学习总结(三)——使用JDK开发WebService

简单的WebService实现
   cxf+spring发布webservice和调用

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-348172-1-1.html 上篇帖子: mvn常用命令 下篇帖子: dubbo_实现Hessian的远程调用协议
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表