CXF入门教程(1) -- 第一个webService
搭建环境
打开你用的最顺手的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,它的作用是向其提交名字的人问好。
[java]
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 inte易做图ce iHelloWorld {
//加入WebParam注解,以保证xml文件中参数名字的正确性
String sayHi(@WebParam(name="text") String text);
/* Advanced usecase of passing an Inte易做图ce in. JAX-WS/JAXB does not
* support inte易做图ces 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。接口实现部分的示例如下:
[java]
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(endpointInte易做图ce = "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接口。
发布服务
[java]
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 = "www.zzzyk.com";
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注解写错了,endpointInte易做图ce属性的值没有写对;改成正确的接口路径之后,程序运行就OK了。
service程序启动之后,用浏览器访问www.zzzyk.com ,如果能够看到类似下图所示的XML文件,就说明我们的第一个webservice已经创建成功了!
作者:NearEast
补充:Web开发 , 其他 ,