CXF入门教程(1) -- 第一个webService

转自:http://blog.csdn.net/neareast?viewmode=contents

项目中要使用webService了;今天上午直接上官网学习CXF,本文来源于对官方文档A simple JAX-WS service的翻译与实践,针对最新的cxf-2.6.1版本调整了所依赖的jar包,并在代码中补全了原文没有提到的一些类;边学边记录,谨防遗忘,顺便分享。

本例与cxf-2.6.1发行版中的示例 java_first_jaxws 相对应,适用于CXF 2.0.1及以上版本。

本文也附上了笔者代码的下载地址,欢迎下载;工程中不包含所引用的jar包,使用者参照文中的截图建立相应的user Library即可使用:

http://download.csdn.net/detail/neareast/4410086

搭建环境

打开你用的最顺手的IDE,创建一个新项目。笔者使用的是Eclipse indigo,创建了一个基本的Java项目。我们要做的第一件事就是在项目中加入CXF所依赖的类。我们可以在CXF的发行版的lib目录找到相应的jar包,注意根据发行版本的不同,jar包的版本号可能有相应的变化:
commons-logging-1.1.1.jar
geronimo-activation_1.1_spec-1.0.2.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.6.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.2.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.2.jar (JSR 181)
geronimo-jaxws_2.1_spec-1.0.jar (or Sun's jaxws-api-2.1.jar)
geronimo-stax-api_1.0_spec-1.0.1.jar (or other stax-api jar)
jaxb-api-2.1.jar
jaxb-impl-2.1.12.jar
jetty-6.1.21.jar
jetty-util-6.1.21.jar
neethi-2.0.4.jar
saaj-api-1.3.jar
saaj-impl-1.3.2.jar
wsdl4j-1.6.2.jar
wstx-asl-3.2.8.jar
XmlSchema-1.4.5.jar
xml-resolver-1.2.jar 

spring的包(可选,需要使用XML配置支持时引入):

aopalliance-1.0.jar
spring-core-2.5.5.jar
spring-beans-2.5.5.jar
spring-context-2.5.5.jar
spring-web-2.5.5.jar

当然,还有最重要的,CXF的包:

cxf-2.2.3.jar
cxf-2.6.1的发行版所带的jar包与上面的清单有较大不同,笔者在Eclipse中创建了一个名为“cxf-dependency”的user Library,最终使用的jar包列表如下图所示:

开始写Service

首先我们写一个serivce接口,其中包含一个操作sayHi,它的作用是向其提交名字的人问好。
  1. package com.neareast.test.cxf.service;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import javax.jws.WebParam;  
  6. import javax.jws.WebService;  
  7. import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;  
  8.   
  9. import com.neareast.test.cxf.bean.User;  
  10. import com.neareast.test.cxf.util.IntegerUserMapAdapter;  
  11.   
  12.   
  13. @WebService  
  14. public interface iHelloWorld {  
  15.   
  16.     //加入WebParam注解,以保证xml文件中参数名字的正确性   
  17.     String sayHi(@WebParam(name="text") String text);  
  18.   
  19.   
  20.     /* Advanced usecase of passing an Interface in.  JAX-WS/JAXB does not 
  21.      * support interfaces directly.  Special XmlAdapter classes need to 
  22.      * be written to handle them 
  23.      */  
  24.     String sayHiToUser(User user);  
  25.   
  26.     /* Map passing 
  27.      * JAXB also does not support Maps.  It handles Lists great, but Maps are 
  28.      * not supported directly.  They also require use of a XmlAdapter to map 
  29.      * the maps into beans that JAXB can use.  
  30.      */  
  31.     @XmlJavaTypeAdapter(IntegerUserMapAdapter.class)  
  32.     Map<Integer, User> getUsers();  
  33. }  
package com.neareast.test.cxf.service;

import java.util.Map;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.neareast.test.cxf.bean.User;
import com.neareast.test.cxf.util.IntegerUserMapAdapter;


@WebService
public interface iHelloWorld {

    //加入WebParam注解,以保证xml文件中参数名字的正确性
    String sayHi(@WebParam(name="text") String text);


    /* Advanced usecase of passing an Interface in.  JAX-WS/JAXB does not
     * support interfaces directly.  Special XmlAdapter classes need to
     * be written to handle them
     */
    String sayHiToUser(User user);

    /* Map passing
     * JAXB also does not support Maps.  It handles Lists great, but Maps are
     * not supported directly.  They also require use of a XmlAdapter to map
     * the maps into beans that JAXB can use. 
     */
    @XmlJavaTypeAdapter(IntegerUserMapAdapter.class)
    Map<Integer, User> getUsers();
}

WebParam注解是必须的,因为java借口编译后的.class文件不保存参数的名字,所以如果没有加注解,参数将被命名为arg0。接口实现部分的示例如下:
  1. package com.neareast.test.cxf.service;  
  2.   
  3. import java.util.LinkedHashMap;  
  4. import java.util.Map;  
  5.   
  6. import javax.jws.WebService;  
  7.   
  8. import com.neareast.test.cxf.bean.User;  
  9.   
  10.   
  11. @WebService(endpointInterface = "com.neareast.test.cxf.service.iHelloWorld", serviceName = "HelloWorld")  
  12. public class HelloWorldImpl implements iHelloWorld {  
  13.     Map<Integer, User> users = new LinkedHashMap<Integer, User>();  
  14.   
  15.     public String sayHi(String text) {  
  16.         System.out.println("sayHi called");  
  17.         return "Hello " + text;  
  18.     }  
  19.   
  20.     public String sayHiToUser(User user) {  
  21.         System.out.println("sayHiToUser called");  
  22.         users.put(users.size() + 1, user);  
  23.         return "Hello " + user.getName();  
  24.     }  
  25.   
  26.     public Map<Integer, User> getUsers() {  
  27.         System.out.println("getUsers called");  
  28.         return users;  
  29.     }  
  30.   
  31. }  
package com.neareast.test.cxf.service;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.jws.WebService;

import com.neareast.test.cxf.bean.User;


@WebService(endpointInterface = "com.neareast.test.cxf.service.iHelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl implements iHelloWorld {
	Map<Integer, User> users = new LinkedHashMap<Integer, User>();

	public String sayHi(String text) {
		System.out.println("sayHi called");
		return "Hello " + text;
	}

	public String sayHiToUser(User user) {
		System.out.println("sayHiToUser called");
		users.put(users.size() + 1, user);
		return "Hello " + user.getName();
	}

	public Map<Integer, User> getUsers() {
		System.out.println("getUsers called");
		return users;
	}

}

@WebService注解让CXF知道我们希望使用哪个接口来创建WSDL,本例中就是iHelloWorld接口。

发布服务

  1. package com.neareast.test.cxf.service;  
  2.   
  3. import javax.xml.ws.Endpoint;  
  4.   
  5. public class ServiceTest {  
  6.   
  7.     protected ServiceTest() throws Exception {  
  8.         // START SNIPPET: publish   
  9.         System.out.println("Starting Server");  
  10.         HelloWorldImpl implementor = new HelloWorldImpl();  
  11.         String address = "http://localhost:9000/helloWorld";  
  12.         Endpoint.publish(address, implementor);  
  13.         // END SNIPPET: publish   
  14.     }  
  15.   
  16.     public static void main(String args[]) throws Exception {  
  17.         new ServiceTest();  
  18.         System.out.println("Server ready...");  
  19.   
  20.         Thread.sleep(5 * 60 * 1000);  
  21.         System.out.println("Server exiting");  
  22.         System.exit(0);  
  23.     }  
  24.   
  25. }  
package com.neareast.test.cxf.service;

import javax.xml.ws.Endpoint;

public class ServiceTest {

	protected ServiceTest() throws Exception {
        // START SNIPPET: publish
        System.out.println("Starting Server");
        HelloWorldImpl implementor = new HelloWorldImpl();
        String address = "http://localhost:9000/helloWorld";
        Endpoint.publish(address, implementor);
        // END SNIPPET: publish
    }

    public static void main(String args[]) throws Exception {
        new ServiceTest();
        System.out.println("Server ready...");

        Thread.sleep(5 * 60 * 1000);
        System.out.println("Server exiting");
        System.exit(0);
    }

}

第一次直接运行的时候,程序报错:

Exception in thread "main" javax.xml.ws.WebServiceException: javax.xml.ws.WebServiceException: Could not load Webservice SEI

检查代码发现,是HelloWorldImpl类的@WebService注解写错了,endpointInterface属性的值没有写对;改成正确的接口路径之后,程序运行就OK了。

service程序启动之后,用浏览器访问  http://localhost:9000/helloWorld?wsdl ,如果能够看到类似下图所示的XML文件,就说明我们的第一个webservice已经创建成功了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值