日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用 CXF 做 webservice 简单例子

發(fā)布時(shí)間:2023/12/13 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用 CXF 做 webservice 简单例子 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn):http://www.cnblogs.com/frankliiu-java/articles/1641949.html

Apache CXF 是一個(gè)開放源代碼框架,提供了用于方便地構(gòu)建和開發(fā) Web 服務(wù)的可靠基礎(chǔ)架構(gòu)。它允許創(chuàng)建高性能和可擴(kuò)展的服務(wù),您可以將這樣的服務(wù)部署在 Tomcat 和基于 Spring 的輕量級(jí)容器中,以及部署在更高級(jí)的服務(wù)器上,例如 Jboss、IBM? WebSphere? 或 BEA WebLogic。

該框架提供了以下功能:

  • Web 服務(wù)標(biāo)準(zhǔn)支持:CXF 支持以下 Web 服務(wù)標(biāo)準(zhǔn):
    • Java API for XML Web Services (JAX-WS)
    • SOAP
    • Web 服務(wù)描述語言(Web Services Description Language ,WSDL)
    • 消息傳輸優(yōu)化機(jī)制(Message Transmission Optimization Mechanism,MTOM)
    • WS-Basic Profile
    • WS-Addressing
    • WS-Policy
    • WS-ReliableMessaging
    • WS-Security
  • 前端建模:CXF 提供了前端建模的概念,允許您使用不同的前端 API 來創(chuàng)建 Web 服務(wù)。API 允許您使用簡(jiǎn)單的工廠 Bean 并通過 JAX-WAS 實(shí)現(xiàn)來創(chuàng)建 Web 服務(wù)。它還允許您創(chuàng)建動(dòng)態(tài) Web 服務(wù)客戶端。
  • 工具支持:CXF 提供了用于在 Java Bean、Web 服務(wù)和 WSDL 之間進(jìn)行轉(zhuǎn)換的不同工具。它提供了對(duì) Maven 和 Ant 集成的支持,并無縫地支持 Spring 集成。
  • RESTful 服務(wù)支持:CXF 支持代表性狀態(tài)傳輸(Representational State Transfer,RESTful )服務(wù)的概念,并支持 Java 平臺(tái)的 JAX-RS 實(shí)現(xiàn)。(本系列的第 2 部分將提供有關(guān) RESTful 服務(wù)的更多信息。)
  • 對(duì)不同傳輸和綁定的支持:CXF 支持不同種類的傳輸,從 XML 到逗號(hào)分隔值 (CSV)。除了支持 SOAP 和 HTTP 協(xié)議綁定之外,它還支持 Java Architecture for XML Binding (JAXB) 和 AEGIS 數(shù)據(jù)綁定。
  • 對(duì)非 XML 綁定的支持:CXF 支持非 XML 綁定,例如 JavaScript Object Notation (JSON) 和 Common Object Request Broker Architecture (CORBA)。它還支持 Java 業(yè)務(wù)集成(Java Business Integration,JBI)體系架構(gòu)和服務(wù)組件體系架構(gòu)(Service Component Architecture,SCA)。
  • code first 或者 xml first:支持使用code first 或者 xml first 的方式來創(chuàng)建web服務(wù)。

一 借助 annotation 創(chuàng)建獨(dú)立啟動(dòng)的web 服務(wù)。

準(zhǔn)備: 新建工程 導(dǎo)入需要的jar 包:

依賴的包:

commons-logging-1.1.jar
geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.1.jar
jaxb-impl-2.1.6.jar
jaxws-api-2.1.jar
jetty-6.1.5.jar
jetty-util-6.1.5.jar
neethi-2.0.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
stax-api-1.0.1.jar
wsdl4j-1.6.1.jar
wstx-asl-3.2.1.jar
XmlSchema-1.2.jar
xml-resolver-1.2.jar

spring jar包, 用來支持xml配置:

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

CXF jar包:

cxf-2.1.jar

以上jar 包 可從apache官方網(wǎng)站下載apache-cxf-2.1.2.zip, 然后從apache-cxf-2.1.2/lib 目錄中獲得

1 首先服務(wù)點(diǎn)接口。

[java]view plaincopy
  • packagecom.demo;
  • importjava.util.List;
  • importjavax.jws.WebParam;
  • importjavax.jws.WebService;
  • @WebService
  • publicinterfaceHelloWorld{
  • StringsayHi(@WebParam(name="text")Stringtext);
  • StringsayHiToUser(Useruser);
  • String[]SayHiToUserList(List<User>userList);
  • }

  • · 2 編寫服務(wù)實(shí)現(xiàn)

    [java]view plaincopy
  • packagecom.demo;
  • importjava.util.LinkedHashMap;
  • importjava.util.List;
  • importjava.util.Map;
  • importjavax.jws.WebService;
  • @WebService(endpointInterface="com.demo.HelloWorld",serviceName="HelloWorld")
  • publicclassHelloWorldImplimplementsHelloWorld{
  • Map<Integer,User>users=newLinkedHashMap<Integer,User>();
  • publicStringsayHi(Stringtext){
  • return"Hello"+text;
  • }
  • publicStringsayHiToUser(Useruser){
  • users.put(users.size()+1,user);
  • return"Hello"+user.getName();
  • }
  • publicString[]SayHiToUserList(List<User>userList){
  • String[]result=newString[userList.size()];
  • inti=0;
  • for(Useru:userList){
  • result[i]="Hello"+u.getName();
  • i++;
  • }
  • returnresult;
  • }
  • }

  • 3 編寫 webServiceApp.java類來暴露 web服務(wù)。

    [java]view plaincopy
  • packagecom.demo;
  • importjavax.xml.ws.Endpoint;
  • publicclasswebServiceApp{
  • publicstaticvoidmain(String[]args){
  • System.out.println("webservicestart");
  • HelloWorldImplimplementor=newHelloWorldImpl();
  • Stringaddress="http://localhost:8080/helloWorld";
  • Endpoint.publish(address,implementor);
  • System.out.println("webservicestarted");
  • }
  • }

  • 4 run webServiceApp.java 類來啟動(dòng)服務(wù)。 訪問http://localhost:8080/helloWorld?wsdl 查看是否顯示

    wsdl。

    5編寫客戶端訪問服務(wù)。

    [java]view plaincopy
  • packagecom.demo;
  • importjava.util.ArrayList;
  • importjava.util.List;
  • importorg.apache.cxf.jaxws.JaxWsProxyFactoryBean;
  • importorg.springframework.context.support.ClassPathXmlApplicationContext;
  • publicclassHelloWorldClient{
  • publicstaticvoidmain(String[]args){
  • JaxWsProxyFactoryBeansvr=newJaxWsProxyFactoryBean();
  • svr.setServiceClass(HelloWorld.class);
  • svr.setAddress("http://localhost:8080/helloWorld");
  • HelloWorldhw=(HelloWorld)svr.create();
  • Useruser=newUser();
  • user.setName("Tony");
  • user.setDescription("test");
  • System.out.println(hw.sayHiToUser(user));
  • }
  • }

  • 6測(cè)試: run webServiceApp.java 類來啟動(dòng)服務(wù),然后 run HelloWorldClient.java 來訪問服務(wù)。


    二 集成到spring 中。

    1 在 web.xml 中加入 :

    [html]view plaincopy
  • <?xmlversion="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>

  • 2 在 applicationContext.xml 中加入:

    [html]view plaincopy
  • <?xmlversion="1.0"encoding="UTF-8"?>
  • <beansxmlns="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/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd">
  • <importresource="classpath:META-INF/cxf/cxf.xml"/>
  • <importresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
  • <importresource="classpath:META-INF/cxf/cxf-servlet.xml"/>
  • <jaxws:endpoint
  • id="helloWorld"
  • implementor="com.demo.HelloWorldImpl"
  • address="/helloWorld"/>
  • <beanid="client"class="com.demo.HelloWorld"
  • factory-bean="clientFactory"factory-method="create"/>
  • <beanid="clientFactory"class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
  • <propertyname="serviceClass"value="com.demo.HelloWorld"/>
  • <propertyname="address"
  • value="http://localhost:8080/s/webservice/helloWorld"/>
  • </bean>
  • </beans>

  • 注意: 這里需要加入 xmlns:jaxws="http://cxf.apache.org/jaxws"和

    http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd

    3 修改客戶端。

    [java]view plaincopy
  • packagecom.demo;
  • importjava.util.ArrayList;
  • importjava.util.List;
  • importorg.apache.cxf.jaxws.JaxWsProxyFactoryBean;
  • importorg.springframework.context.support.ClassPathXmlApplicationContext;
  • publicclassHelloWorldClient{
  • publicstaticvoidmain(String[]args){
  • HelloWorldclient=(HelloWorld)context.getBean("client");
  • Useruser1=newUser();
  • user1.setName("Tony");
  • user1.setDescription("test");
  • Useruser2=newUser();
  • user2.setName("freeman");
  • user2.setDescription("test");
  • List<User>userList=newArrayList<User>();
  • userList.add(user1);
  • userList.add(user2);
  • String[]res=client.SayHiToUserList(userList);
  • System.out.println(res[0]);
  • System.out.println(res[1]);
  • }
  • }

  • 4 發(fā)布工程啟動(dòng)web服務(wù)器(我用 tomcat 6)。

    5訪問http://localhost:8080/s/webservice/helloWorld?wsdl查看是否顯示 wsdl 。

    6run run HelloWorldClient.java 來訪問服務(wù)。

    總結(jié)

    以上是生活随笔為你收集整理的使用 CXF 做 webservice 简单例子的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。