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

Webservice_19_SOAP的基于契约优先WSDL的开发流程

发布时间:2020-12-17 00:11:35 所属栏目:安全 来源:网络整理
导读:非常感谢孙浩老师。 ? 先创建wsdl: ?xml version="1.0" encoding="UTF-8" standalone="no"?wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:tns="http://www.example.org/mywsdl/"xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/

非常感谢孙浩老师。

?

先创建wsdl:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://www.example.org/mywsdl/"
	xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
	xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
	name="MyServiceImplService"
	targetNamespace="http://www.example.org/mywsdl/">
	<wsdl:types>
		<xsd:schema targetNamespace="http://www.example.org/mywsdl/">
			<xsd:element name="add" type="tns:addType" />
			<xsd:element name="addResponse" type="tns:addResponseType" />
			<xsd:element name="minus" type="tns:minusType" />
			<xsd:element name="minusResponse" type="tns:minusResponseType" />

			<xsd:complexType name="addType">
				<xsd:sequence>
					<xsd:element name="a" type="xsd:int" />
					<xsd:element name="b" type="xsd:int" />
				</xsd:sequence>
			</xsd:complexType>
			<xsd:complexType name="addResponseType">
				<xsd:sequence>
					<xsd:element name="addResult" type="xsd:int" />
				</xsd:sequence>
			</xsd:complexType>
			<xsd:complexType name="minusType">
				<xsd:sequence>
					<xsd:element name="num1" type="xsd:int" />
					<xsd:element name="num2" type="xsd:int" />
				</xsd:sequence>
			</xsd:complexType>
			<xsd:complexType name="minusResponseType">
				<xsd:sequence>
					<xsd:element name="minusResult" type="xsd:int" />
				</xsd:sequence>
			</xsd:complexType>

		</xsd:schema>
	</wsdl:types>

	<wsdl:message name="add">
		<wsdl:part element="tns:add" name="add" />
	</wsdl:message>
	<wsdl:message name="addResponse">
		<wsdl:part element="tns:addResponse" name="addResponse" />
	</wsdl:message>
	<wsdl:message name="minus">
		<wsdl:part element="tns:minus" name="minus" />
	</wsdl:message>
	<wsdl:message name="minusResponse">
		<wsdl:part element="tns:minusResponse" name="minusResponse" />
	</wsdl:message>

	<wsdl:portType name="IMyService">
		<wsdl:operation name="add">
			<wsdl:input message="tns:add" />
			<wsdl:output message="tns:addResponse" />
		</wsdl:operation>
		<wsdl:operation name="minus">
			<wsdl:input message="tns:minus" />
			<wsdl:output message="tns:minusResponse" />
		</wsdl:operation>
	</wsdl:portType>

	<wsdl:binding name="myServiceSOAP" type="tns:IMyService">
		<soap:binding style="document"
			transport="http://schemas.xmlsoap.org/soap/http" />
		<wsdl:operation name="add">
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="minus">
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:service name="MyServiceImplService">
		<wsdl:port binding="tns:myServiceSOAP" name="MyServiceImplPort">
			<soap:address location="http://localhost:8888/ms" />
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>


使用wsimport,得到服务端的Java文件,就拷贝IMyService.java到服务器项目中。

实现IMyService的实现类:

package org.example.mywsdl;

import javax.jws.WebService;

@WebService(endpointInterface = "org.example.mywsdl.IMyService",targetNamespace = "http://www.example.org/mywsdl/",wsdlLocation = "META-INF/wsdl/mywsdl.wsdl")
public class MyServiceImpl implements IMyService {

	public MyServiceImpl() {
	}

	@Override
	public int add(int a,int b) {
		System.out.println(a + "+" + b + "=" + (a + b));
		return (a + b);
	}

	@Override
	public int minus(int num1,int num2) {
		System.out.println(num1 + "-" + num2 + "=" + (num1 - num2));
		return (num1 - num2);
	}

}


?

创建服务:

?

package org.example.mywsdl;

import javax.xml.ws.Endpoint;

public class MyService {
	public static void main(String[] args) {
		Endpoint.publish("http://localhost:8888/ms",new MyServiceImpl());
	}
}


?

最终服务端项目的构成:

?

?

在使用wsimport得到客户端Java文件,把所有Java文件拷贝到客户端的项目中。

在运行测试类:

public static void main(String[] args) {
		try {
			URL url = new URL("http://localhost:8888/ms?wsdl");
			QName name = new QName("http://www.example.org/mywsdl/","MyServiceImplService");
			MyServiceImplService implService = new MyServiceImplService(url,name);
			IMyService ms = implService.getMyServiceImplPort();
			System.out.println(ms.minus(33,21));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}


客户端控制台:

服务端控制台:

(编辑:李大同)

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

    推荐文章
      热点阅读