webservice简单入门总结

来源:互联网 发布:linux 删除history 编辑:程序博客网 时间:2024/05/17 23:27

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的客户端不只是能调用服务器的方法也可以调用实体对象 但是实体对象要进行序列化


0 0