apache cxf 编写web service接口样例程序

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.yeetrack</groupId>
  <artifactId>mavenWeb</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>mavenWeb Maven Webapp</name>
  <properties>
    <cxf.version>2.7.3</cxf.version>
  </properties>

  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-plugin-api</artifactId>
    <version>3.0.5</version>

    </dependency>
    <dependency>
        <groupId>org.apache.geronimo.specs</groupId>
        <artifactId>geronimo-servlet_2.5_spec</artifactId>
        <version>1.2</version>
    </dependency>

    <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>
        <!-- Jetty is needed if you're are not using the CXFServlet -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http-jetty</artifactId>
        <version>${cxf.version}</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>  
  </dependencies>
  <build>
    <finalName>mavenWeb</finalName>
    <plugins>
    <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.10</version>
        <configuration>
          <stopPort>9966</stopPort>
          <stopKey>foo</stopKey>  
        </configuration>
    </plugin>
</plugins>
  </build>
</project>

 

下面来看看HelloWorld的具体例子。

 

1.创建HelloWorld 接口类

package com.yeetrack.cxf;  

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.yeetrack.cxf;  

public class HelloWorldImpl implements HelloWorld {  

    public String sayHi(String name) {  
        String msg = "Hello " + name + "!";  
        return msg;  
    }  
}

 

3.创建Server端测试类

 

package com.yeetrack.cxf;  

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.yeetrack.cxf;

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("kongxx"));
        System.exit(0);
    }
}

 

5.测试

 

首先运行Server类来启动Web Service服务,然后访问http://localhost:9000/ws/HelloWorld?wsdl地址来确定web service启动正确。

运行Client测试类,会在命令行输出Hello kongxx!的message。

Apache cxf 测试WebService接口

来源:csdn之kongxx的专栏

 

 

版权声明

本站文章、图片、视频等(除转载外),均采用知识共享署名 4.0 国际许可协议(CC BY-NC-SA 4.0),转载请注明出处、非商业性使用、并且以相同协议共享。

© 空空博客,本文链接:https://www.yeetrack.com/?p=15