程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 使用Apache CXF創建Web Service

使用Apache CXF創建Web Service

編輯:關於JAVA

官方主頁:http://cxf.apache.org/

下載後內附詳細的sample。這裡先演示一個最簡單的工程。

如下圖所示建立工程:

客戶端測試代碼:

TestServiceClient.java

1.package jp.co.apm.client;
2.
3.import jp.co.apm.service.TestService;
4.
5.import org.apache.cxf.frontend.ClientProxyFactoryBean;
6.
7.public class TestServiceClient {
8.
9.    public static void main(String[] args) {
10.
11.        ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
12.        factory.setServiceClass(TestService.class);
13.        factory.setAddress("http://localhost:8080/APM_CXF/services/test");
14.
15.        TestService service = (TestService) factory.create();
16.        System.out.println(service.sayHello());
17.    }
18.}

TestServiceImpl.java

1.package jp.co.apm.service.impl;
2.
3.import jp.co.apm.service.TestService;
4.
5.public class TestServiceImpl implements TestService {
6.
7.    public String sayHello() {
8.
9.        return "Hello, Shen Bin";
10.    }
11.}

TestService.java

1.package jp.co.apm.service;
2.
3.public interface TestService {
4.
5.    public String sayHello();
6.}

cxf-servlet.xml

1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans xmlns="http://www.springframework.org/schema/beans"
3.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4.    xmlns:simple="http://cxf.apache.org/simple"
5.    xmlns:soap="http://cxf.apache.org/bindings/soap"
6.    xsi:schemaLocation="
7.http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
8.http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
9.http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd">
10.
11.    <simple:server id="testservice" serviceClass="jp.co.apm.service.TestService" address="/test">
12.        <simple:serviceBean>
13.            <bean class="jp.co.apm.service.impl.TestServiceImpl" />
14.        </simple:serviceBean>
15.    </simple:server>
16.
17.</beans>

web.xml

1.<?xml version="1.0" encoding="UTF-8"?>
2.<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
3.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4.    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
5.    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
6.
7.    <display-name>APM</display-name>
8.    <description>APM</description>9.
10.    <servlet>
11.        <servlet-name>APM</servlet-name>
12.        <servlet-class>
13.            org.apache.cxf.transport.servlet.CXFServlet
14.        </servlet-class>
15.        <load-on-startup>2</load-on-startup>
16.    </servlet>
17.
18.    <servlet-mapping>
19.        <servlet-name>APM</servlet-name>
20.        <url-pattern>/services/*</url-pattern>
21.    </servlet-mapping>
22.
23.    <session-config>
24.        <session-timeout>60</session-timeout>
25.    </session-config>
26.
27.</web-app>

啟動Tomcat,訪問: http://localhost:8080/APM_CXF/services/test?wsdl

運行TestServiceClient.java測試結果。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved