<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 28,  comments - 13,  trackbacks - 0
    現(xiàn)在的項(xiàng)目中需要用到SOA概念的地方越來越多,最近我接手的一個(gè)項(xiàng)目中就提出了這樣的業(yè)務(wù)要求,需要在.net開發(fā)的客戶端系統(tǒng)中訪問java開發(fā)的web系統(tǒng),這樣的業(yè)務(wù)需求自然需要通過WebService進(jìn)行信息數(shù)據(jù)的操作。下面就將我們?cè)陂_發(fā)中摸索的一點(diǎn)經(jīng)驗(yàn)教訓(xùn)總結(jié)以下,以供大家參考.

    我們項(xiàng)目的整個(gè)架構(gòu)使用的比較流行的WSH MVC組合,即webwork2 + Spring + Hibernate;
    1.首先集成Apacha CXF WebService 到 Spring 框架中;
    apache cxf 下載地址:http://people.apache.org/dist/incubator/cxf/2.0.4-incubator/apache-cxf-2.0.4-incubator.zip
    在spring context配置文件中引入以下cxf配置
    	<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" />

    在web.xml中添加過濾器:
    	<servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>
    org.apache.cxf.transport.servlet.CXFServlet
    </servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
    </servlet-mapping>


    2.開發(fā)服務(wù)端WebService接口:
    /**
    * WebService接口定義類.
    *
    * 使用@WebService將接口中的所有方法輸出為Web Service.
    * 可用annotation對(duì)設(shè)置方法、參數(shù)和返回值在WSDL中的定義.
    */
    @WebService
    public interface WebServiceSample {


    /**
    * 一個(gè)簡(jiǎn)單的方法,返回一個(gè)字符串
    * @param hello
    * @return
    */
    String say(String hello);

    /**
    * 稍微復(fù)雜一些的方法,傳遞一個(gè)對(duì)象給服務(wù)端處理
    * @param user
    * @return
    */
    String sayUserName(
    @WebParam(name = "user")
    UserDTO user);

    /**
    * 最復(fù)雜的方法,返回一個(gè)List封裝的對(duì)象集合
    * @return
    */
    public
    @WebResult(partName="o")
    ListObject findUsers();

    }

    由簡(jiǎn)單到復(fù)雜定義了三個(gè)接口,模擬業(yè)務(wù)需求;

    3.實(shí)現(xiàn)接口
    /**
    * WebService實(shí)現(xiàn)類.
    *
    * 使用@WebService指向Interface定義類即可.
    */
    @WebService(endpointInterface = "cn.org.coral.biz.examples.webservice.WebServiceSample")
    public class WebServiceSampleImpl implements WebServiceSample {

    public String sayUserName(UserDTO user) {
    return "hello "+user.getName();
    }

    public String say(String hello) {
    return "hello "+hello;
    }

    public ListObject findUsers() {
    ArrayList<Object> list = new ArrayList<Object>();

    list.add(instancUser(1,"lib"));
    list.add(instancUser(2,"mld"));
    list.add(instancUser(3,"lq"));
    list.add(instancUser(4,"gj"));
    ListObject o = new ListObject();
    o.setList(list);
    return o;
    }

    private UserDTO instancUser(Integer id,String name){
    UserDTO user = new UserDTO();
    user.setId(id);
    user.setName(name);
    return user;
    }
    }


    4.依賴的兩個(gè)類:用戶對(duì)象與List對(duì)象
    /**
    * Web Service傳輸U(kuò)ser信息的DTO.
    *
    * 分離entity類與web service接口間的耦合,隔絕entity類的修改對(duì)接口的影響.
    * 使用JAXB 2.0的annotation標(biāo)注JAVA-XML映射,盡量使用默認(rèn)約定.
    *
    */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "User")
    public class UserDTO {

    protected Integer id;

    protected String name;

    public Integer getId() {
    return id;
    }

    public void setId(Integer value) {
    id = value;
    }

    public String getName() {
    return name;
    }

    public void setName(String value) {
    name = value;
    }
    }

    關(guān)于List對(duì)象,參照了有關(guān)JWS的一個(gè)問題中的描述:DK6.0 自帶的WebService中 WebMethod的參數(shù)好像不能是ArrayList 或者其他List
    傳遞List需要將List 包裝在其他對(duì)象內(nèi)部才行 (個(gè)人理解 如有不對(duì)請(qǐng)指出) ,我在實(shí)踐中也遇到了此類問題.通過以下封裝的對(duì)象即可以傳遞List對(duì)象.
    /**
    * <p>Java class for listObject complex type.
    *
    * <p>The following schema fragment specifies the expected content contained within this class.
    *
    * <pre>
    * <complexType name="listObject">
    * <complexContent>
    * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
    * <sequence>
    * <element name="list" type="{http://www.w3.org/2001/XMLSchema}anyType" maxOccurs="unbounded" minOccurs="0"/>
    * </sequence>
    * </restriction>
    * </complexContent>
    * </complexType>
    * </pre>
    *
    *
    */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "listObject", propOrder = { "list" })
    public class ListObject {

    @XmlElement(nillable = true)
    protected List<Object> list;

    /**
    * Gets the value of the list property.
    *
    * <p>
    * This accessor method returns a reference to the live list,
    * not a snapshot. Therefore any modification you make to the
    * returned list will be present inside the JAXB object.
    * This is why there is not a <CODE>set</CODE> method for the list property.
    *
    * <p>
    * For example, to add a new item, do as follows:
    * <pre>
    * getList().add(newItem);
    * </pre>
    *
    *
    * <p>
    * Objects of the following type(s) are allowed in the list
    * {@link Object }
    *
    *
    */
    public List<Object> getList() {
    if (list == null) {
    list = new ArrayList<Object>();
    }
    return this.list;
    }

    public void setList(ArrayList<Object> list) {
    this.list = list;
    }

    }


    5.WebService 服務(wù)端 spring 配置文件 ws-context.xml
    <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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd"
    default-autowire="byName" default-lazy-init="true">

    <jaxws:endpoint id="webServiceSample"
    address="/WebServiceSample" implementor="cn.org.coral.biz.examples.webservice.WebServiceSampleImpl"/>

    </beans>


    WebService 客戶端 spring 配置文件 wsclient-context.xml
    <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://cxf.apache.org/jaxws
    http://cxf.apache.org/schemas/jaxws.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd"
    default-autowire="byName" default-lazy-init="true">

    <!-- ws client -->
    <bean id="identityValidateServiceClient" class="cn.org.coral.admin.service.IdentityValidateService"
    factory-bean="identityValidateServiceClientFactory" factory-method="create" />

    <bean id="identityValidateServiceClientFactory"
    class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
    <property name="serviceClass"
    value="cn.org.coral.admin.service.IdentityValidateService" />
    <property name="address"
    value="http://88.148.29.54:8080/coral/services/IdentityValidateService"/>
    </bean>

    </beans>

    6.發(fā)布到tomcat服務(wù)器以后通過以下地址即可查看自定義的webservice接口生成的wsdl:
    http://88.148.29.54:8080/aio/services/WebServiceSample?wsdl

    7.調(diào)用WebService接口的Junit單元測(cè)試程序
    package test.coral.sample;

    import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

    import cn.org.coral.biz.examples.webservice.WebServiceSample;
    import cn.org.coral.biz.examples.webservice.dto.UserDTO;

    public class TestWebServiceSample extends
    AbstractDependencyInjectionSpringContextTests {
    WebServiceSample webServiceSampleClient;

    public void setWebServiceSampleClient(WebServiceSample webServiceSampleClient) {
    this.webServiceSampleClient = webServiceSampleClient;
    }

    @Override
    protected String[] getConfigLocations() {
    setAutowireMode(AUTOWIRE_BY_NAME);
    //spring 客戶端配置文件保存位置
    return new String[] { "classpath:/cn/org/coral/biz/examples/webservice/wsclient-context.xml" };
    }

    public void testWSClinet(){
    Assert.hasText(webServiceSampleClient.say(" world"));
    }
    }

    posted on 2008-03-16 21:40 Lib 閱讀(39143) 評(píng)論(5)  編輯  收藏 所屬分類: Java


    FeedBack:
    # Virusnyi Marketing
    2009-05-18 08:18 | Virusnyi Marketing
    Hi everyone. Our deeds determine us, as much as we determine our deeds.
    I am from Vietnam and learning to write in English, give true I wrote the following sentence: "Seo india wildnet technologies is india based seo india company, a seo india company providing cheap seo services in india.Search engine optimization pay per click ppc management post click marketing."

    With respect :P, Kala.  回復(fù)  更多評(píng)論
      
    # re: WebService開發(fā)筆記 1 -- 利用cxf開發(fā)WebService竟然如此簡(jiǎn)單
    2011-08-02 16:14 | 靚女購(gòu)物街
    最近項(xiàng)目用到CXF做開發(fā),學(xué)習(xí)下,謝謝  回復(fù)  更多評(píng)論
      
    # re: WebService開發(fā)筆記 1 -- 利用cxf開發(fā)WebService竟然如此簡(jiǎn)單
    2011-11-23 11:04 | wanxkl
    不錯(cuò)。,謝謝了  回復(fù)  更多評(píng)論
      
    # re: WebService開發(fā)筆記 1 -- 利用cxf開發(fā)WebService竟然如此簡(jiǎn)單
    2012-06-07 16:30 | 鵝鵝鵝
    公司的分公司的  回復(fù)  更多評(píng)論
      
    # re: WebService開發(fā)筆記 1 -- 利用cxf開發(fā)WebService竟然如此簡(jiǎn)單
    2014-12-22 15:05 | sd
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567



    我的JavaEye博客
    http://lib.javaeye.com


    常用鏈接

    留言簿(2)

    隨筆分類

    文章分類

    FLASH

    Java

    搜索

    •  

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 国产L精品国产亚洲区久久| 亚洲精品影院久久久久久| 四虎国产精品永久免费网址| 亚洲六月丁香六月婷婷色伊人| 精品久久洲久久久久护士免费| 久久er国产精品免费观看8| 亚洲成人福利在线观看| 免费国产a国产片高清网站| 日韩视频在线观看免费| 亚洲AV无码成人网站在线观看| 亚洲国产第一站精品蜜芽| 午夜视频免费观看| 国产羞羞的视频在线观看免费| 亚洲无人区码一二三码区别图片 | 亚洲不卡AV影片在线播放| 久久久久久久久久国产精品免费| 亚洲gay片在线gv网站| 亚洲日韩区在线电影| 亚洲国产午夜中文字幕精品黄网站 | 亚洲无人区码一二三码区别图片 | 精品剧情v国产在免费线观看| 国产在线精品免费aaa片| 亚洲gay片在线gv网站| 亚洲日韩乱码中文无码蜜桃 | 亚洲国产精品无码久久久秋霞1| 亚洲成色www久久网站夜月| 成人免费无码精品国产电影| 国产黄色免费网站| 欧洲人成在线免费| 一级成人a免费视频| 亚洲AV噜噜一区二区三区| 亚洲导航深夜福利| 亚洲国产高清视频| 亚洲精品乱码久久久久久蜜桃不卡 | 亚洲大尺度无码无码专区| 亚洲日本一区二区一本一道| 岛国大片免费在线观看| 99视频全部免费精品全部四虎| 国产一精品一av一免费爽爽 | 日韩特黄特色大片免费视频| 很黄很黄的网站免费的|