WebService的开发模式
一、 客户端开发
(1) 利用cxf+Spring的方式,创建服务类,调用服务方法,获取服务器信息。这种方式操作简单,但是需要服务器中服务类的接口。而且还要在工程了导入大量的包。在安卓应用中不推荐使用。
实现步骤:在利用cxf开放webservice一文中提到过。
(2) 利用java的HttpURLConnection访问服务器,这种方法最原始,需要对conn设定请求头信息,而且还要组装soap消息。处理回应也需要解析soap消息才能获取服务器提供的信息。
实现方法如下:
1、创建webservice,假如服务端的路径为:http://localhost:8000/cxf_service1/helloService
2、创建一个客户端类
package zxf.cxf.service; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class SimpleClient { public static void main(String[] args) { try { URL url=new URL("http://localhost:8000/cxf_service1/helloService"); HttpURLConnection conn=(HttpURLConnection)url.openConnection(); conn.setRequestMethod("POST"); conn.setConnectTimeout(5000); conn.setDoOutput(true); conn.setRequestProperty("Content-Type","text/xml; charset=UTF-8"); conn.setRequestProperty("SOAPAction","\"\""); conn.setRequestProperty("Accept","*/*"); OutputStream out=conn.getoutputStream(); //组装soap消息 String s="<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"+ "<soap:Body><ns2:say xmlns:ns2='http://service.cxf.zxf/'><arg0>ii</arg0>"+ "</ns2:say></soap:Body></soap:Envelope>"; out.write(s.getBytes()); out.close(); if(conn.getResponseCode()==200){ System.out.println("ok"); InputStream in=conn.getInputStream(); byte[] b=new byte[1024]; in.read(b); String s1=new String(b);
//得到soap消息 System.out.println(s1); //解析soap消息 } } catch (Exception e) { e.printstacktrace(); } } }
|
运行之后TCP/IP监听器的结果如下:
(3) 利用jdk(6.0以上)的包中的类去组装soap消息,解析回应的soap消息,不要spring的类。这种方式必须要知道服务的描述文档(wsdl)需要引入一个service接口SayHello
实现的步骤如下:
1、 服务描述文档如下:
<?xml version='1.0' encoding='UTF-8'?><wsdl:deFinitions name="SayHelloImplService" targetNamespace="http://service.cxf.zxf/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://service.cxf.zxf/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <wsdl:types> <xs:schema elementFormDefault="unqualified" targetNamespace="http://service.cxf.zxf/" version="1.0" xmlns:tns="http://service.cxf.zxf/" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="say" type="tns:say"/> <xs:element name="sayResponse" type="tns:sayResponse"/> <xs:complexType name="say"> <xs:sequence> <xs:element minOccurs="0" name="arg0" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="sayResponse"> <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema> </wsdl:types> <wsdl:message name="sayResponse"> <wsdl:part element="tns:sayResponse" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:message name="say"> <wsdl:part element="tns:say" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:portType name="SayHello"> <wsdl:operation name="say"> <wsdl:input message="tns:say" name="say"> </wsdl:input> <wsdl:output message="tns:sayResponse" name="sayResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="SayHelloImplServiceSoapBinding" type="tns:SayHello"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="say"> <soap:operation soapAction="" style="document"/> <wsdl:input name="say"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="sayResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="SayHelloImplService"> <wsdl:port binding="tns:SayHelloImplServiceSoapBinding" name="SayHelloImplPort"> <soap:address location="http://localhost:8080/cxf_service1/helloService"/> </wsdl:port> </wsdl:service> </wsdl:deFinitions> |
2、 客户端代码如下:
package zxf.cxf.service; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; public class CxfClient { public static void main(String[] args) { //wsdl服务描述文档的地址 String wsdl="http://localhost:8080/cxf_service1/helloService?wsdl"; //名字空间 String namespace="http://service.cxf.zxf/"; //服务名的限定名对象 QName serviceName=new QName(namespace,"SayHelloImplService"); //端口的限定名 QName portname=new QName(namespace,"SayHelloImplPort"); try { URL url=new URL(wsdl); //创建服务对象 Service service=Service.create(url,serviceName); //通过服务对象创建端口对象 SayHello s=service.getPort(portname,SayHello.class); System.out.println(s.say("dfdff")); } catch (Exception e) { e.printstacktrace(); }
} } |
二、 服务器端开发
(1) 代码优先(先开发服务,然后得到wsdl文档)
有两种开发方式:
1、 利用jdk对webservice的支持,开发webservice。代码如下:
package zxf.service; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.soAPBinding; import javax.jws.soap.soAPBinding.Style; import javax.xml.ws.Endpoint; import javax.xml.ws.ServiceMode; @WebService @SOAPBinding(style=Style.RPC) public class SayHello { @WebMethod public String hello(String name){ System.out.println("hello"); return "hello "+name; } public static void main(String [] s){ String url="http://localhost:8080/welcomeService"; Endpoint.publish(url,new SayHello()); } } |
2、 利用cxf+spring开发
此方法在利用cxf+spring开发webservice中提到过。
(2) 服务优先(先定义文档,然后开发服务)
要使用一个命令:比较复杂
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。