CXF+Spring+Maven+JBOSS7簡單例子

Apache CXF 是一個開放源代碼框架,提供了用於方便地構建和開發 Web 服務的可靠基礎架構。它容許建立高性能和可擴展的服務,您能夠將這樣的服務部署在 Tomcat 和基於 Spring 的輕量級容器中,以及部署在更高級的服務器上,例如 Jboss、IBM® WebSphere® 或 BEA WebLogic。

 

       該框架提供瞭如下功能: 前端

  • Web 服務標準支持:CXF 支持如下 Web 服務標準:
    • Java API for XML Web Services (JAX-WS)
    • SOAP
    • Web 服務描述語言(Web Services Description Language ,WSDL)
    • 消息傳輸優化機制(Message Transmission Optimization Mechanism,MTOM)
    • WS-Basic Profile
    • WS-Addressing
    • WS-Policy
    • WS-ReliableMessaging
    • WS-Security
  • 前端建模:CXF 提供了前端建模的概念,容許您使用不一樣的前端 API 來建立 Web 服務。API 容許您使用簡單的工廠 Bean 並經過 JAX-WAS 實現來建立 Web 服務。它還容許您建立動態 Web 服務客戶端。
  • 工具支持:CXF 提供了用於在 Java Bean、Web 服務和 WSDL 之間進行轉換的不一樣工具。它提供了對 Maven 和 Ant 集成的支持,並沒有縫地支持 Spring 集成。
  • RESTful 服務支持:CXF 支持表明性狀態傳輸(Representational State Transfer,RESTful )服務的概念,並支持 Java 平臺的 JAX-RS 實現。(本系列的第 2 部分將提供有關 RESTful 服務的更多信息。)
  • 對不一樣傳輸和綁定的支持:CXF 支持不一樣種類的傳輸,從 XML 到逗號分隔值 (CSV)。除了支持 SOAP 和 HTTP 協議綁定以外,它還支持 Java Architecture for XML Binding (JAXB) 和 AEGIS 數據綁定。
  • 對非 XML 綁定的支持:CXF 支持非 XML 綁定,例如 JavaScript Object Notation (JSON) 和 Common Object Request Broker Architecture (CORBA)。它還支持 Java 業務集成(Java Business Integration,JBI)體系架構和服務組件體系架構(Service Component Architecture,SCA)。
  • code first 或者 xml first  : 支持使用code first 或者 xml first 的方式來建立web服務。

1.建立一個web工程,這是必須的,Maven建立時選擇war方式。 java

2.在Maven的pom.xml準備好全部的CXF2.1和Spring3的jar. web

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>CXF</groupId>
  <artifactId>CXF</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>CXF</name>
  <description>CXF</description>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
  <dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>

<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-activation_1.1_spec</artifactId>
<version>1.0-M1</version>
</dependency>

<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-annotation_1.0_spec</artifactId>
<version>1.1</version>
</dependency>

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

<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-ws-metadata_2.0_spec</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
     <groupId>javax.xml.bind</groupId>
     <artifactId>jaxb-api</artifactId>
     <version>2.1</version>
   </dependency>
   
   <dependency>
     <groupId>javax.xml.ws</groupId>
     <artifactId>jaxws-api</artifactId>
     <version>2.1</version>
   </dependency>
   
   <dependency>
     <groupId>javax.xml.soap</groupId>
     <artifactId>saaj-api</artifactId>
     <version>1.3</version>
   </dependency>
   
   <dependency>
     <groupId>javax.xml.soap</groupId>
     <artifactId>saaj-impl</artifactId>
     <version>1.3</version>
   </dependency>
   
   <dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.wsdl4j-1.6.1</artifactId>
<version>4.0-m1</version>
</dependency>

<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>wstx-asl</artifactId>
<version>3.2.1</version>
</dependency>

<dependency>
  <groupId>org.apache.ws.commons.schema</groupId>
  <artifactId>XmlSchema</artifactId>
  <version>1.4.7</version>
</dependency>

<dependency>
<groupId>xml-resolver</groupId>
<artifactId>xml-resolver</artifactId>
<version>1.2</version>
</dependency>

<dependency>
   <groupId>org.apache.ws.xmlschema</groupId>
   <artifactId>xmlschema-core</artifactId>
   <version>2.0.1</version>
</dependency>

<dependency>
<groupId>org.apache.neethi</groupId>
<artifactId>neethi</artifactId>
<version>2.0.5</version>
</dependency>

<dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>2.1</version>
        </dependency>


        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>2.1</version>
        </dependency>
        
        
        <!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.2.RELEASE</version>
<scope>test</scope>
</dependency>
  </dependencies>
</project>

spring

3.創建一個接口: apache

package com.demo;

import java.util.List;

import javax.jws.WebParam;
import javax.jws.WebService;

import com.demo.User;

@WebService
public interface HelloWorld {
String sayHi(@WebParam(name="text")String text);
    String sayHiToUser(User user);
    String[] SayHiToUserList(List<User> userList);
}
api

4.創建實現接口類
package com.demo;

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

import javax.jws.WebService;

import com.demo.User;

@WebService(endpointInterface = "com.demo.HelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {

Map<Integer, User> users = new LinkedHashMap<Integer, User>();

@Override
public String sayHi(String text) {
// TODO Auto-generated method stub
return "Hello " + text;
}

@Override
public String sayHiToUser(User user) {
// TODO Auto-generated method stub
users.put(users.size() + 1, user);
return "Hello " + user.getName();
}

@Override
public String[] SayHiToUserList(List<User> userList) {
// TODO Auto-generated method stub
String[] result = new String[userList.size()];
int i = 0;
for (User u : userList) {
result[i] = "Hello " + u.getName();
i++;
}
return result;
}
}
瀏覽器


5.創建本地測試服務端,服務端監聽調用
package com.demo;

import javax.xml.ws.Endpoint;

public class WebServiceApp {

public static void main(String[] args) {
        System.out.println("web service start");
        HelloWorldImpl implementor= new HelloWorldImpl();
        String address="http://localhost:8080/helloWorld";
        Endpoint.publish(address, implementor);
        System.out.println("web service started");
}
}


6.創建本地測試客戶端 服務器

package com.demo;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.demo.User;

public class HelloWorldClient {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
        svr.setServiceClass(HelloWorld.class);
        svr.setAddress("http://localhost:8080/helloWorld");
        HelloWorld hw = (HelloWorld) svr.create();
        User user = new User();
        user.setName("Tony");
        user.setDescription("test");
        System.out.println(hw.sayHi(user.getName()));
}
}
架構


7.先開啓本地測試服務端代碼,等待服務開啓成功後,處於監聽狀態,再開啓本地測試客戶端代碼。這時測試成功調用後返回
Hello Tony
本地環境測試成功。


8.接着部署web.xml的Spring環境
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXFServlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>

mvc

9.把spring的applicationContext.xml配置文件也相應寫好
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
                       http://www.springframework.org/schema/beans

                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<jaxws:endpoint id="helloWorld" implementor="com.demo.HelloWorldImpl"
address="/helloWorld" />

<bean id="client" class="com.demo.HelloWorld" factory-bean="clientFactory"
factory-method="create" />

<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.demo.HelloWorld" />
<property name="address"
value="http://localhost:8080/CXF/webservice/helloWorld" />
</bean>
</beans>

10.把項目部署到jboss-as-7.1.1.Final,開啓jboss,讓服務啓動成功後。

能夠進入測試頁面看是否註冊了webservice.

把地址輸入到瀏覽器
http://localhost:8080/CXF/webservice/helloWorld?wsdl



<?xml version="1.0" encoding="UTF-8"?>
-<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://demo.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://demo.com/" name="HelloWorld"> -<wsdl:types> -<xs:schema xmlns:tns="http://demo.com/" targetNamespace="http://demo.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified"> <xs:element name="SayHiToUserList" type="tns:SayHiToUserList"/> <xs:element name="SayHiToUserListResponse" type="tns:SayHiToUserListResponse"/> <xs:element name="sayHi" type="tns:sayHi"/> <xs:element name="sayHiResponse" type="tns:sayHiResponse"/> <xs:element name="sayHiToUser" type="tns:sayHiToUser"/> <xs:element name="sayHiToUserResponse" type="tns:sayHiToUserResponse"/> -<xs:complexType name="sayHiToUser"> -<xs:sequence> <xs:element name="arg0" type="tns:user" minOccurs="0"/> </xs:sequence> </xs:complexType> -<xs:complexType name="user"> -<xs:sequence> <xs:element name="description" type="xs:string" minOccurs="0"/> <xs:element name="name" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> -<xs:complexType name="sayHiToUserResponse"> -<xs:sequence> <xs:element name="return" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> -<xs:complexType name="sayHi"> -<xs:sequence> <xs:element name="text" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> -<xs:complexType name="sayHiResponse"> -<xs:sequence> <xs:element name="return" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> -<xs:complexType name="SayHiToUserList"> -<xs:sequence> <xs:element name="arg0" type="tns:user" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> -<xs:complexType name="SayHiToUserListResponse"> -<xs:sequence> <xs:element name="return" type="xs:string" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:schema> </wsdl:types> -<wsdl:message name="sayHiToUserResponse"> <wsdl:part name="parameters" element="tns:sayHiToUserResponse"> </wsdl:part> </wsdl:message> -<wsdl:message name="SayHiToUserList"> <wsdl:part name="parameters" element="tns:SayHiToUserList"> </wsdl:part> </wsdl:message> -<wsdl:message name="SayHiToUserListResponse"> <wsdl:part name="parameters" element="tns:SayHiToUserListResponse"> </wsdl:part> </wsdl:message> -<wsdl:message name="sayHi"> <wsdl:part name="parameters" element="tns:sayHi"> </wsdl:part> </wsdl:message> -<wsdl:message name="sayHiToUser"> <wsdl:part name="parameters" element="tns:sayHiToUser"> </wsdl:part> </wsdl:message> -<wsdl:message name="sayHiResponse"> <wsdl:part name="parameters" element="tns:sayHiResponse"> </wsdl:part> </wsdl:message> -<wsdl:portType name="HelloWorld"> -<wsdl:operation name="sayHiToUser"> <wsdl:input name="sayHiToUser" message="tns:sayHiToUser"> </wsdl:input> <wsdl:output name="sayHiToUserResponse" message="tns:sayHiToUserResponse"> </wsdl:output> </wsdl:operation> -<wsdl:operation name="sayHi"> <wsdl:input name="sayHi" message="tns:sayHi"> </wsdl:input> <wsdl:output name="sayHiResponse" message="tns:sayHiResponse"> </wsdl:output> </wsdl:operation> -<wsdl:operation name="SayHiToUserList"> <wsdl:input name="SayHiToUserList" message="tns:SayHiToUserList"> </wsdl:input> <wsdl:output name="SayHiToUserListResponse" message="tns:SayHiToUserListResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType> -<wsdl:binding name="HelloWorldSoapBinding" type="tns:HelloWorld"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> -<wsdl:operation name="sayHiToUser"> <soap:operation style="document" soapAction=""/> -<wsdl:input name="sayHiToUser"> <soap:body use="literal"/> </wsdl:input> -<wsdl:output name="sayHiToUserResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> -<wsdl:operation name="sayHi"> <soap:operation style="document" soapAction=""/> -<wsdl:input name="sayHi"> <soap:body use="literal"/> </wsdl:input> -<wsdl:output name="sayHiResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> -<wsdl:operation name="SayHiToUserList"> <soap:operation style="document" soapAction=""/> -<wsdl:input name="SayHiToUserList"> <soap:body use="literal"/> </wsdl:input> -<wsdl:output name="SayHiToUserListResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> -<wsdl:service name="HelloWorld"> -<wsdl:port name="HelloWorldImplPort" binding="tns:HelloWorldSoapBinding"> <soap:address location="http://localhost:8080/CXF/webservice/helloWorld"/> </wsdl:port> </wsdl:service> </wsdl:definitions>

服務已經註冊成功

11.再寫一個客戶端調用測試
package com.demo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloWorldClient2 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld client = (HelloWorld)context.getBean("client");
        User user1 = new User();
        user1.setName("Tony!@#");
        user1.setDescription("test111");
        User user2 = new User();
        user2.setName("freeman334");
        user2.setDescription("test");
        List<User> userList= new ArrayList<User>();
        userList.add(user1);
        userList.add(user2);
        String[] res = client.SayHiToUserList(userList);
        System.out.println(res[0]);
        System.out.println(res[1]);          
}
}

運行測試返回 Hello Tony!@# Hello freeman334 測試成功!祝你好運!

相關文章
相關標籤/搜索