Apache CXF官方网站的A Simple JAX-WS Service依葫芦画瓢走了一遍,没有太多东东,这里仅仅是记录下来已方便自己回忆。这里我使用Eclipse作为开发工具。
根据官方网站可以把这个Sample分成四部分来完成,所以我这里也按照这四部分来记录:
1. 搭建Apache CXF环境
a) 创建一个新的Java Project
b) 添加相关的jar包,所有的jar包都可以在CXF官方下载站点的apache-cxf-2.2.8.zip下载包里找到.
CXF依赖的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
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依赖的jar包:
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
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的jar包:
cxf-2.2.3.jar
2. 编写Service端代码
a) Service端接口类:
- import javax.jws.WebParam;
- import javax.jws.WebService;
- @WebService
- public interface HelloService {
- public String sayHello(@WebParam(name="text")String text);
- }
b) Service端实现类:
- import javax.jws.WebService;
- @WebService
- public class HelloServiceImpl implements HelloService {
- public String sayHello(String text) {
- return "Hello ".concat(text);
- }
- }
3. 发布Service端代码
a) 发布代码
- protected Server() throws Exception {
- // START SNIPPET: publish
- System.out.println("Starting Server");
- HelloServiceImpl implementor = new HelloServiceImpl ();
- String address = "http://localhost:9000/helloWorld";
- Endpoint.publish(address, implementor );
- // END SNIPPET: publish
- }
b) Main 函数
- 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);
- }
c) 运行Server端代码,并用浏览器打开“http://localhost:9000/helloWorld?wsdl”来验证WSDL是否生产。
4. 编写Client端代码
a) Client端代码:
- public static void main(String[] args) {
- JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
- factory.setServiceClass(HelloService.class);
- factory.setAddress("http://localhost:9000/helloWorld");
- HelloService client = (HelloService) factory.create();
- String reply = client.sayHello("aaa");
- System.out.println("Server said :".concat(reply));
- System.exit(0);
- }
b) 先启动服务器端代码,在启动Client端代码,可看到输出为“Hello aaa”.
没有评论:
发表评论