athrunwang

          紀元
          數據加載中……
          簡單的基于CXF框架發布webserivce服務
          前段時間在弄各種框架下的webservice,要弄demo,網上搜羅了許多,都講得很好,但感覺就是說得很多很復雜,我就想弄個服務出來供我用一下, 要像網上那么做覺著太麻煩,后來參考各路神仙大佬們后,把代碼極度縮小,寫了個小實例供自個跑著玩,順便代碼貼上,供大伙口水
          支持包:cxf框架lib下核心包,自己官網上下去,在這里就不貼了,除此之外有java環境1.6+就Ok了,其它略過,上代碼了。
          package com.cxf.bean;

          public class Employee {
              private String id;
              private String name;
              private String address;

              public String getId() {
                  return id;
              }

              public void setId(String id) {
                  this.id = id;
              }

              public String getName() {
                  return name;
              }

              public void setName(String name) {
                  this.name = name;
              }

              public String getAddress() {
                  return address;
              }

              public void setAddress(String address) {
                  this.address = address;
              }

          }
          package com.cxf.service;

          import javax.jws.WebService;

          import com.cxf.bean.Employee;

          @WebService
          public interface EmployeeService {
              public boolean insertEmployee(Employee e);

              public boolean updateEmployee(String id);

              public boolean deleteEmployee(String id);

              public Employee seletctEmployee(String id);
          }
          package com.cxf.service;

          import javax.jws.WebService;

          import com.cxf.bean.Employee;

          @WebService
          public class EmployeeServiceImpl implements EmployeeService {

              @Override
              public boolean insertEmployee(Employee e) {
                  //業務想咋整自已實現去吧,我這里作例子,就直接回true了,呵呵
                  return true;
              }

              @Override
              public boolean updateEmployee(String id) {
                  //業務想咋整自已實現去吧,我這里作例子,就直接回true了,呵呵
                  return true;
              }

              @Override
              public boolean deleteEmployee(String id) {
                  //業務想咋整自已實現去吧,我這里作例子,就直接回true了,呵呵
                  return true;
              }

              @Override
              public Employee seletctEmployee(String id) {
                  Employee e = new Employee();
                  e.setAddress("http://業務想咋整自已實現去吧,我這里作例子,就直接回true了,呵呵");
                  e.setId("http://業務想咋整自已實現去吧,我這里作例子,就直接回true了,呵呵");
                  e.setName("http://業務想咋整自已實現去吧,我這里作例子,就直接回true了,呵呵");
                  return e;
              }

          }
          package test;
          import org.apache.cxf.endpoint.Server;
          import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
          import com.cxf.service.EmployeeServiceImpl;
          public class MainServer {
              public static void main(String[] args) {         
                  JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
                  factory.setServiceClass(EmployeeServiceImpl.class);
                  factory.setAddress("http://192.9.11.53:8088/employeeService");         
                  Server server = factory.create();
                  server.start();
              }
          }
          package test;

          import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

          import com.cxf.bean.Employee;
          import com.cxf.service.EmployeeService;

          public class EmployeeServiceClient {
              public static void main(String[] args) {
                  JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
                  factory.setAddress("http://192.9.11.53:8088/employeeService");
                  factory.setServiceClass(EmployeeService.class);
                  EmployeeService es = (EmployeeService) factory.create();
                  Employee e = new Employee();
                  e= es.seletctEmployee("id");
                  System.out.println("地址:"+e.getAddress()+"         姓名:"+e.getName()+"         編號:"+e.getId());
                  System.out.println(es.seletctEmployee("test"));
              }
          }
          //在eclipse里跑發布類后瀏覽器中訪問http://192.9.11.53:8088/employeeService?wsdl得到描述wsdl
          <wsdl:definitions name="EmployeeServiceImplService" targetNamespace="http://service.cxf.com/"><wsdl:types><xs:schema elementFormDefault="unqualified" targetNamespace="http://service.cxf.com/" version="1.0"><xs:element name="deleteEmployee" type="tns:deleteEmployee"/><xs:element name="deleteEmployeeResponse" type="tns:deleteEmployeeResponse"/><xs:element name="insertEmployee" type="tns:insertEmployee"/><xs:element name="insertEmployeeResponse" type="tns:insertEmployeeResponse"/><xs:element name="seletctEmployee" type="tns:seletctEmployee"/><xs:element name="seletctEmployeeResponse" type="tns:seletctEmployeeResponse"/><xs:element name="updateEmployee" type="tns:updateEmployee"/><xs:element name="updateEmployeeResponse" type="tns:updateEmployeeResponse"/><xs:complexType name="updateEmployee"><xs:sequence><xs:element minOccurs="0" name="arg0" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="updateEmployeeResponse"><xs:sequence><xs:element name="return" type="xs:boolean"/></xs:sequence></xs:complexType><xs:complexType name="insertEmployee"><xs:sequence><xs:element minOccurs="0" name="arg0" type="tns:employee"/></xs:sequence></xs:complexType><xs:complexType name="employee"><xs:sequence><xs:element minOccurs="0" name="address" type="xs:string"/><xs:element minOccurs="0" name="id" type="xs:string"/><xs:element minOccurs="0" name="name" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="insertEmployeeResponse"><xs:sequence><xs:element name="return" type="xs:boolean"/></xs:sequence></xs:complexType><xs:complexType name="deleteEmployee"><xs:sequence><xs:element minOccurs="0" name="arg0" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="deleteEmployeeResponse"><xs:sequence><xs:element name="return" type="xs:boolean"/></xs:sequence></xs:complexType><xs:complexType name="seletctEmployee"><xs:sequence><xs:element minOccurs="0" name="arg0" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="seletctEmployeeResponse"><xs:sequence><xs:element minOccurs="0" name="return" type="tns:employee"/></xs:sequence></xs:complexType></xs:schema></wsdl:types><wsdl:message name="insertEmployeeResponse"><wsdl:part element="tns:insertEmployeeResponse" name="parameters">
              </wsdl:part></wsdl:message><wsdl:message name="updateEmployee"><wsdl:part element="tns:updateEmployee" name="parameters">
              </wsdl:part></wsdl:message><wsdl:message name="insertEmployee"><wsdl:part element="tns:insertEmployee" name="parameters">
              </wsdl:part></wsdl:message><wsdl:message name="updateEmployeeResponse"><wsdl:part element="tns:updateEmployeeResponse" name="parameters">
              </wsdl:part></wsdl:message><wsdl:message name="deleteEmployeeResponse"><wsdl:part element="tns:deleteEmployeeResponse" name="parameters">
              </wsdl:part></wsdl:message><wsdl:message name="seletctEmployee"><wsdl:part element="tns:seletctEmployee" name="parameters">
              </wsdl:part></wsdl:message><wsdl:message name="seletctEmployeeResponse"><wsdl:part element="tns:seletctEmployeeResponse" name="parameters">
              </wsdl:part></wsdl:message><wsdl:message name="deleteEmployee"><wsdl:part element="tns:deleteEmployee" name="parameters">
              </wsdl:part></wsdl:message><wsdl:portType name="EmployeeService"><wsdl:operation name="updateEmployee"><wsdl:input message="tns:updateEmployee" name="updateEmployee">
              </wsdl:input><wsdl:output message="tns:updateEmployeeResponse" name="updateEmployeeResponse">
              </wsdl:output></wsdl:operation><wsdl:operation name="insertEmployee"><wsdl:input message="tns:insertEmployee" name="insertEmployee">
              </wsdl:input><wsdl:output message="tns:insertEmployeeResponse" name="insertEmployeeResponse">
              </wsdl:output></wsdl:operation><wsdl:operation name="deleteEmployee"><wsdl:input message="tns:deleteEmployee" name="deleteEmployee">
              </wsdl:input><wsdl:output message="tns:deleteEmployeeResponse" name="deleteEmployeeResponse">
              </wsdl:output></wsdl:operation><wsdl:operation name="seletctEmployee"><wsdl:input message="tns:seletctEmployee" name="seletctEmployee">
              </wsdl:input><wsdl:output message="tns:seletctEmployeeResponse" name="seletctEmployeeResponse">
              </wsdl:output></wsdl:operation></wsdl:portType><wsdl:binding name="EmployeeServiceImplServiceSoapBinding" type="tns:EmployeeService"><soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/><wsdl:operation name="insertEmployee"><soap:operation soapAction="" style="document"/><wsdl:input name="insertEmployee"><soap:body use="literal"/></wsdl:input><wsdl:output name="insertEmployeeResponse"><soap:body use="literal"/></wsdl:output></wsdl:operation><wsdl:operation name="updateEmployee"><soap:operation soapAction="" style="document"/><wsdl:input name="updateEmployee"><soap:body use="literal"/></wsdl:input><wsdl:output name="updateEmployeeResponse"><soap:body use="literal"/></wsdl:output></wsdl:operation><wsdl:operation name="deleteEmployee"><soap:operation soapAction="" style="document"/><wsdl:input name="deleteEmployee"><soap:body use="literal"/></wsdl:input><wsdl:output name="deleteEmployeeResponse"><soap:body use="literal"/></wsdl:output></wsdl:operation><wsdl:operation name="seletctEmployee"><soap:operation soapAction="" style="document"/><wsdl:input name="seletctEmployee"><soap:body use="literal"/></wsdl:input><wsdl:output name="seletctEmployeeResponse"><soap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="EmployeeServiceImplService"><wsdl:port binding="tns:EmployeeServiceImplServiceSoapBinding" name="EmployeeServiceImplPort"><soap:address location="http://192.9.11.53:8088/employeeService"/></wsdl:port></wsdl:service></wsdl:definitions>

          posted on 2011-12-28 20:54 AthrunWang 閱讀(772) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 聊城市| 泗阳县| 中山市| 土默特左旗| 时尚| 江川县| 安吉县| 萨迦县| 北安市| 宾川县| 江山市| 嘉兴市| 浦县| 天等县| 潞西市| 安龙县| 平昌县| 石阡县| 弥渡县| 绥江县| 湘阴县| 平阴县| 平湖市| 科尔| 宝兴县| 木兰县| 米林县| 南京市| 平安县| 平舆县| 台湾省| 太和县| 黄龙县| 沈阳市| 万宁市| 巴青县| 容城县| 绥化市| 东兰县| 沐川县| 仲巴县|