Apache的CXF现在几乎成了Java领域构建Web Service的首选类库,并且它也确实简单易用,下面就通过几篇系列文章做一下简单介绍。
当然首先想到的当然还是那个Hello World示例。这个系列文章中用到的例子都是基于Maven构建的工程,下面是我的pom.xml文件内容
- <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/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.googlecode.garbagecan.cxfstudy</groupId>
- <artifactId>cxfstudy</artifactId>
- <packaging>war</packaging>
- <version>1.0-SNAPSHOT</version>
- <name>cxfstudy Maven Webapp</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>cxfstudy</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>
下面来看看HelloWorld的具体例子。
1.创建HelloWorld 接口类
- package com.googlecode.garbagecan.cxfstudy.helloworld;
- import javax.jws.WebMethod;
- import javax.jws.WebParam;
- import javax.jws.WebResult;
- import javax.jws.WebService;
- @WebService
- public interface HelloWorld {
- @WebMethod
- @WebResult String sayHi(@WebParam String text);
- }
2.创建HelloWorld实现类
- package com.googlecode.garbagecan.cxfstudy.helloworld;
- public class HelloWorldImpl implements HelloWorld {
- public String sayHi(String name) {
- String msg = "Hello " + name + "!";
- return msg;
- }
- }
3.创建Server端测试类
- package com.googlecode.garbagecan.cxfstudy.helloworld;
- import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
- // http://localhost:9000/HelloWorld?wsdl
- public class Server {
- public static void main(String[] args) throws Exception {
- JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
- factory.setServiceClass(HelloWorldImpl.class);
- factory.setAddress("http://localhost:9000/ws/HelloWorld");
- factory.create();
- System.out.println("Server start...");
- Thread.sleep(60 * 1000);
- System.out.println("Server exit...");
- System.exit(0);
- }
- }
4.创建Client端测试类
- package com.googlecode.garbagecan.cxfstudy.helloworld;
- import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
- public class Client {
- public static void main(String[] args) {
- JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
- factory.setServiceClass(HelloWorld.class);
- factory.setAddress("http://localhost:9000/ws/HelloWorld");
- HelloWorld helloworld = (HelloWorld) factory.create();
- System.out.println(helloworld.sayHi("xx"));
- System.exit(0);
- }
- }
5.测试
首先运行Server类来启动Web Service服务,然后访问http://localhost:9000/ws/HelloWorld?wsdl地址来确定web service启动正确。
运行Client测试类,会在命令行输出Hello xx!的message。
相关推荐
apache cxf 一个helloworld的例子
使用apache cxf开发一个入门小程序
NULL 博文链接:https://itway.iteye.com/blog/1039986
cxf2.1.3 web项目Helloworld,在tomcat6.0可以跑,相关lib可以从cxf.apache.org下载.
Hello World Rest 使用 Apache Cxf 和 Spring Framework
webservice-helloworld 视频 两台机器访问,一个是虚拟机。
webservice-helloworld 视频 两台机器访问,一个是虚拟机。 高清
Camel / CXF Gradle 示例这是一个使用 Apache Camel 和 Apache CXF 的 Hello World 项目公开了一个简单的 Web 服务 HelloService.wsdl。 构建并运行主类。 网址是 通过 SOAP-UI 进行测试。
helloworld_jax_ws,a demo for apache cxf ,u can use it to run....
wsdl2java源码Apache CXF 是一个开源服务框架,可帮助使用前端编程 API(如 JAX-WS)构建和开发服务。 在本教程中,我们将了解如何将 CXF 与 Spring Boot 集成以构建和运行 Hello World SOAP 服务。 在整个示例中,...
网络服务 一个java webService项目,使用spring / mybatis / apache cxf。 1个数据库配置 ... 我写了一个helloworld文件:src \ main \ java \ com \ neusoft \ webservices \ impl \ TjQueryImpl.java
每当收到请求时,helloWorld 服务都会对请求进行一些简单的转换,然后对 exampleGoodbye 进行直接虚拟机调用,并传递有效负载。 exampleGoodbye 服务执行一个简单的转换并将转换后的有效负载返回给 exampleHello,...