这个例子非常简单,servlet容器没有用到tomcat,是嵌入jetty作为容器简单的运行一下而已,并通过maven管理项目。
我下载的是apache-cxf-3.0.2。(如果要用eclipce运行程序,就需要把该文件夹下的所有jar包导入。其实我也不知道那些jar是需要的那些不需要,全导入简单 粗暴 有效果 呵呵。)
目录结构
1、编写服务端程序
public interface HelloWorld {
String sayHi(String text);
}
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
System.out.println("sayHi called and text:"+text);
return "Hello " + text;
}
}
2、发布服务
//import org.apache.cxf.aegis.databinding.AegisDatabinding;
import org.apache.cxf.frontend.ServerFactoryBean;
public class Server {
protected Server() throws Exception {
HelloWorldImpl helloworldImpl = new HelloWorldImpl();
ServerFactoryBean svrFactory = new ServerFactoryBean();
svrFactory.setServiceClass(HelloWorld.class);
svrFactory.setAddress("http://192.168.0.121:9000/Hello");//ge
svrFactory.setServiceBean(helloworldImpl);
//svrFactory.getServiceFactory().setDataBinding(new AegisDatabinding());
svrFactory.create();
}
public static void main(String args[]) throws Exception {
new Server();
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}
</pre><pre name="code" class="cpp"><pre name="code" class="plain">
3、客户端调用程序
package main.java.demo.hw.client;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
import main.java.demo.hw.server.HelloWorld;
public final class Client {
private Client() {
}
public static void main(String args[]) throws Exception {
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
if (args != null && args.length > 0 && !"".equals(args[0])) {
factory.setAddress(args[0]);
} else {
factory.setAddress("http://192.168.0.121:9000/Hello");<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">//根据自己的主机地址而定</span>
}
HelloWorld client = factory.create(HelloWorld.class);
System.out.println("Invoke sayHi()....");
System.out.println(client.sayHi("JP"));
System.exit(0);
}
}
4、pom.xml文件,该文件放在项目根目录下,内容:
</pre><pre name="code" class="html"><?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<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>
<artifactId>java_first_pojo</artifactId>
<name>Java First POJO Sample</name>
<description>Java First POJO Sample</description>
<parent>
<groupId>org.apache.cxf.samples</groupId>
<artifactId>cxf-samples</artifactId>
<version>3.0.2</version>
</parent>
<properties>
<cxf.version>${project.version}</cxf.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>server</id>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>demo.hw.server.Server</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>client</id>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>demo.hw.client.Client</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.0.2</version>
</dependency>
<!-- Jetty is needed if you're using the CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
</project>
5、然后执行
mvn install
6、编译成功后执行
mvn -Pserver
可以看到是jetty容器启动了该webservice。在浏览器输入http://192.168.0.121:9000?Hello?wsdl,会显示wsdl文档,表示成功。
另打开一个cmd窗口,cd到项目中,运行
mvn -Pclient
调用成功!
eclipce 调用也一样。