Webservice_19_SOAP的基于契约优先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();
		}
	}


客户端控制台:

服务端控制台:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值