webservice簡單入門總結

webservice簡單運行原理:


webservice的作用:解決異構軟件系統之間的通信。

基於Myeclipse開發webservice基於xfire的簡單實現

1)創建一個web工程

    1:添加xfire的jar包


這個過程做了三個事情:

  1:添加進來了兩個jar包


  2:創建了一個webservice文件夾


  3:在web.xml中添加了一個servlet



以上就是在服務器端做的三個事情

  我們在服務器端要用webservice 要面向接口編程

  所以我創建了一個接口和它的實現類


然後在services.xml配置我們的接口和實現類

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<!-- 名稱 -->
<name>hello</name>
<!-- 接口的路徑 -->
<serviceClass>cn.com.demo.hello.IHello</serviceClass>
<!-- 實現類的路徑 -->
<implementationClass>cn.com.demo.hello.impl.HelloImpl</implementationClass>
<!-- 服務的生命週期 -->
<scope>application</scope>
</service>
</beans>

做完上述步驟之後就驗證webservice是否發佈成功

1)將寫好的web工程加入到tomcat容器中

2)啓動tomcat容器

3)通過瀏覽器訪問web.xml中配置的servlet-ur

l

然後點擊wsdl就會看到

<wsdl:definitions xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="http://hello.demo.com.cn" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" targetNamespace="http://hello.demo.com.cn">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://hello.demo.com.cn">
<xsd:element name="hello">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="helloResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="helloRequest">
<wsdl:part name="parameters" element="tns:hello"></wsdl:part>
</wsdl:message>
<wsdl:message name="helloResponse">
<wsdl:part name="parameters" element="tns:helloResponse"></wsdl:part>
</wsdl:message>
<wsdl:portType name="helloPortType">
<wsdl:operation name="hello">
<wsdl:input name="helloRequest" message="tns:helloRequest"></wsdl:input>
<wsdl:output name="helloResponse" message="tns:helloResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="helloHttpBinding" type="tns:helloPortType">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="hello">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="helloRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="helloResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="hello">
<wsdl:port name="helloHttpPort" binding="tns:helloHttpBinding">
<wsdlsoap:address location="http://localhost:8080/WebServiceDemo/services/hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

紅色字體部分就是實現類當中的方法

<xsd:element name="hello">就是方法名 

<xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string"/>形參類型

<xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string"/>返回值類型

 <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>這個很重要 就是客戶端訪問uddi所需要的URL但是要

寫成這樣http://localhost:8080/WebServiceDemo/services/hello?wsdl

2)然後寫客戶端

 1:首先創建一個java工程的客戶端不是web工程

@1:加入xfire  jar包



這次要勾選上HTTP這個jar包

右鍵點擊src再創建一個webserviceClient


然後選擇Xfire:


然後下一步:


WSDL URL 就是前面用綠色字體寫的那個URL

在創建一個新的包名 點擊next 然後就產生如下幾個文件


這是helloClient的代碼



package cn.com.client;


import java.net.MalformedURLException;
import java.util.Collection;
import java.util.HashMap;
import javax.xml.namespace.QName;
import org.codehaus.xfire.XFireRuntimeException;
import org.codehaus.xfire.aegis.AegisBindingProvider;
import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.jaxb2.JaxbTypeRegistry;
import org.codehaus.xfire.service.Endpoint;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.soap.AbstractSoapBinding;
import org.codehaus.xfire.transport.TransportManager;


public class helloClient {


    private static XFireProxyFactory proxyFactory = new XFireProxyFactory();
    private HashMap endpoints = new HashMap();
    private Service service0;


    public helloClient() {
        create0();
        Endpoint helloPortTypeLocalEndpointEP = service0 .addEndpoint(new QName("http://hello.demo.com.cn", "helloPortTypeLocalEndpoint"), new QName("http://hello.demo.com.cn", "helloPortTypeLocalBinding"), "xfire.local://hello");
        endpoints.put(new QName("http://hello.demo.com.cn", "helloPortTypeLocalEndpoint"), helloPortTypeLocalEndpointEP);
        Endpoint helloHttpPortEP = service0 .addEndpoint(new QName("http://hello.demo.com.cn", "helloHttpPort"), new QName("http://hello.demo.com.cn", "helloHttpBinding"), "http://localhost:8080/WebServiceDemo/services/hello");
        endpoints.put(new QName("http://hello.demo.com.cn", "helloHttpPort"), helloHttpPortEP);
    }


    public Object getEndpoint(Endpoint endpoint) {
        try {
            return proxyFactory.create((endpoint).getBinding(), (endpoint).getUrl());
        } catch (MalformedURLException e) {
            throw new XFireRuntimeException("Invalid URL", e);
        }
    }


    public Object getEndpoint(QName name) {
        Endpoint endpoint = ((Endpoint) endpoints.get((name)));
        if ((endpoint) == null) {
            throw new IllegalStateException("No such endpoint!");
        }
        return getEndpoint((endpoint));
    }


    public Collection getEndpoints() {
        return endpoints.values();
    }


    private void create0() {
        TransportManager tm = (org.codehaus.xfire.XFireFactory.newInstance().getXFire().getTransportManager());
        HashMap props = new HashMap();
        props.put("annotations.allow.interface", true);
        AnnotationServiceFactory asf = new AnnotationServiceFactory(new Jsr181WebAnnotations(), tm, new AegisBindingProvider(new JaxbTypeRegistry()));
        asf.setBindingCreationEnabled(false);
        service0 = asf.create((cn.com.client.helloPortType.class), props);
        {
            AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://hello.demo.com.cn", "helloHttpBinding"), "http://schemas.xmlsoap.org/soap/http");
        }
        {
            AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://hello.demo.com.cn", "helloPortTypeLocalBinding"), "urn:xfire:transport:local");
        }
    }


    public helloPortType gethelloPortTypeLocalEndpoint() {
        return ((helloPortType)(this).getEndpoint(new QName("http://hello.demo.com.cn", "helloPortTypeLocalEndpoint")));
    }


    public helloPortType gethelloPortTypeLocalEndpoint(String url) {
        helloPortType var = gethelloPortTypeLocalEndpoint();
        org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
        return var;
    }


    public helloPortType gethelloHttpPort() {
        return ((helloPortType)(this).getEndpoint(new QName("http://hello.demo.com.cn", "helloHttpPort")));
    }


    public helloPortType gethelloHttpPort(String url) {
        helloPortType var = gethelloHttpPort();
        org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
        return var;
    }


    public static void main(String[] args) {
        


        helloClient client = new helloClient();
        
    helloPortType service = client.gethelloHttpPort();

        

 String result = service.hello("zhangsan");


System.out.println("test client completed");
        System.exit(0);
    }


}

紅色字體很重要 創建一個helloClient 對象 通過這個對象來生成一個helloPortType對象來調用服務器的方法

這個就是webservice和不是web工程之間的通信

2)再做一個和web工程通信的列子此web工程集成了sutrts2框架


步驟和一般web工程一樣 添加該添加的jar 和wenservice集成的時候和前面不是web工程一樣關鍵是怎麼調用webservice中的方法

package cn.com.demo.action;


import cn.com.web.client.helloClient;
import cn.com.web.client.helloPortType;


public class HelloAction {
private String message;
private String userName;

public String getUserName() {
return userName;
}


public void setUserName(String userName) {
this.userName = userName;
}


public String getMessage() {
return message;
}


public void setMessage(String message) {
this.message = message;
}



public String hello(){
String result = "success";
helloClient client = new helloClient();
helloPortType hello = client.gethelloHttpPort();
message = hello.hello(userName);

return result;
}
}

看到這裏我們就發現了一個問題 就是不管怎麼用 都是要創建客戶端的實列 然後通過客戶端來訪問service的方法

今天就寫到這裏吧  實在是脖子太痛了  第一次寫技術博客 下次有時間在寫基於jax-ws的Demo

webservice的客戶端不只是能調用服務器的方法也可以調用實體對象 但是實體對象要進行序列化


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章