加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice的开发模式

发布时间:2020-12-17 00:33:26 所属栏目:安全 来源:网络整理
导读:WebService的开发模式 一、 ????????????? 客户端开发 (1) ?????????? 利用 cxf+Spring 的方式,创建服务类,调用服务方法,获取服务器信息。这种方式操作简单,但是需要服务器中服务类的接口。而且还要在工程了导入大量的包。在安卓应用中不推荐使用。 实

WebService的开发模式

一、?????????????客户端开发

(1)??????????利用cxf+Spring的方式,创建服务类,调用服务方法,获取服务器信息。这种方式操作简单,但是需要服务器中服务类的接口。而且还要在工程了导入大量的包。在安卓应用中不推荐使用。

实现步骤:在利用cxf开放webservice一文中提到过。

(2)??????????利用javaHttpURLConnection访问服务器,这种方法最原始,需要对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、? 利用jdkwebservice的支持,开发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)?????? 服务优先(先定义文档,然后开发服务)

要使用一个命令:比较复杂

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读