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

Webservice_21_SOAP的基于契约优先开发用户管理_实现Wsdl

发布时间:2020-12-17 00:11:31 所属栏目:安全 来源:网络整理
导读:非常感谢孙浩老师。 先编写schema,user.xsd: ?xml version="1.0" encoding="UTF-8"?xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"targetNamespace="http://service.lichen.cn" xmlns:tns="http://service.lichen.cn"elementFormDefault="quali

非常感谢孙浩老师。

先编写schema,user.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	targetNamespace="http://service.lichen.cn" xmlns:tns="http://service.lichen.cn"
	elementFormDefault="qualified">

	<xsd:element name="add" type="tns:addType" />
	<xsd:element name="addResponse" type="tns:addResponseType" />
	<xsd:element name="delete" type="tns:deleteType" />
	<xsd:element name="deleteResponse" type="tns:deleteResponseType" />
	<xsd:element name="list" type="tns:listType" />
	<xsd:element name="listResponse" type="tns:listResponseType" />
	<xsd:element name="login" type="tns:loginType" />
	<xsd:element name="loginResponse" type="tns:loginResponseType" />

	<xsd:complexType name="addType">
		<xsd:sequence>
			<xsd:element name="user" type="tns:user" />
		</xsd:sequence>
	</xsd:complexType>
	<xsd:complexType name="addResponseType">
		<xsd:sequence />
	</xsd:complexType>
	<xsd:complexType name="deleteType">
		<xsd:sequence>
			<xsd:element name="username" type="xsd:string" />
		</xsd:sequence>
	</xsd:complexType>
	<xsd:complexType name="deleteResponseType">
		<xsd:sequence />
	</xsd:complexType>
	<xsd:complexType name="listType">
		<xsd:sequence />
	</xsd:complexType>
	<xsd:complexType name="listResponseType">
		<xsd:sequence minOccurs="1" maxOccurs="unbounded">
			<xsd:element name="user" type="tns:user" />
		</xsd:sequence>
	</xsd:complexType>
	<xsd:complexType name="loginType">
		<xsd:sequence>
			<xsd:element name="username" type="xsd:string" />
			<xsd:element name="passwrod" type="xsd:string" />
		</xsd:sequence>
	</xsd:complexType>
	<xsd:complexType name="loginResponseType">
		<xsd:sequence>
			<xsd:element name="user" type="tns:user" />
		</xsd:sequence>
	</xsd:complexType>
	<xsd:complexType name="user">
		<xsd:sequence>
			<xsd:element name="username" type="xsd:string" />
			<xsd:element name="nickname" type="xsd:string" />
			<xsd:element name="passwrod" type="xsd:string" />
		</xsd:sequence>
	</xsd:complexType>
</xsd:schema>


?

编写wsdl:user.wsdl

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://service.lichen.cn" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="UserService"
	targetNamespace="http://service.lichen.cn">
	<wsdl:types>
		<xsd:schema targetNamespace="http://service.lichen.cn">
			<xsd:include schemaLocation="user.xsd"></xsd:include>
		</xsd:schema>
	</wsdl:types>

	<wsdl:message name="add">
		<wsdl:part element="tns:add" name="parameters" />
	</wsdl:message>
	<wsdl:message name="addResponse">
		<wsdl:part element="tns:addResponse" name="parameters" />
	</wsdl:message>
	<wsdl:message name="delete">
		<wsdl:part element="tns:delete" name="parameters" />
	</wsdl:message>
	<wsdl:message name="deleteResponse">
		<wsdl:part element="tns:deleteResponse" name="parameters" />
	</wsdl:message>
	<wsdl:message name="login">
		<wsdl:part element="tns:login" name="parameters" />
	</wsdl:message>
	<wsdl:message name="loginResponse">
		<wsdl:part element="tns:loginResponse" name="parameters" />
	</wsdl:message>
	<wsdl:message name="list">
		<wsdl:part element="tns:list" name="parameters" />
	</wsdl:message>
	<wsdl:message name="listResponse">
		<wsdl:part element="tns:listResponse" name="parameters" />
	</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="delete">
			<wsdl:input message="tns:delete" />
			<wsdl:output message="tns:deleteResponse" />
		</wsdl:operation>
		<wsdl:operation name="login">
			<wsdl:input message="tns:login" />
			<wsdl:output message="tns:loginResponse" />
		</wsdl:operation>
		<wsdl:operation name="list">
			<wsdl:input message="tns:list" />
			<wsdl:output message="tns:listResponse" />
		</wsdl:operation>
	</wsdl:portType>

	<wsdl:binding name="userServiceSOAP" 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="delete">
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="login">
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="list">
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:service name="UserService">
		<wsdl:port binding="tns:userServiceSOAP" name="UserServicePort">
			<soap:address location="http://localhost:8800/us/" />
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>


使用wsimport得到Java文件,把接口IMyService.java拷贝到服务端项目中(删除IMyService中报错的注解)。

?

由于IMyService.java需要User.java,在服务端中新建:

package cn.lichen.entity;

import java.io.Serializable;

public class User implements Serializable{

	private static final long serialVersionUID = 1L;
	private String username;
	private String nickname;
	private String password;
	public User() {
	}
	
	public User(String username,String nickname,String password) {
		super();
		this.username = username;
		this.nickname = nickname;
		this.password = password;
	}

	public String getUsername() {
		return username;
	}
	public String getNickname() {
		return nickname;
	}
	public String getPassword() {
		return password;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
}


?

添加接口实现类,还没有实现功能,注意类名上的注解必须要:

?

package cn.lichen.service;

import java.util.List;

@WebService(endpointInterface = "cn.lichen.service.IMyService",wsdlLocation = "META-INF/wsdl/user.wsdl",portName = "UserServicePort",serviceName = "UserService",targetNamespace = "http://service.lichen.cn")
public class MyServiceImpl implements IMyService {

	@Override
	public void add(User user) {

	}

	@Override
	public void delete(String username) {

	}

	@Override
	public User login(String username,String passwrod) {
		return null;
	}

	@Override
	public List<User> list() {
		return null;
	}

}


?

在服务端新建服务方法:

?

package cn.lichen.service;

import javax.xml.ws.Endpoint;

public class MyService {

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


?

启动服务后,可成功访问http://localhost:8800/us?wsdl。但使有wsimport导出Java会报错,需要把user.xsd和user.wsdl整合。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://service.lichen.cn" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="UserService"
	targetNamespace="http://service.lichen.cn">
	<wsdl:types>
		<xsd:schema targetNamespace="http://service.lichen.cn">
			<!-- <xsd:include schemaLocation="user.xsd"></xsd:include> -->

			<xsd:element name="add" type="tns:addType" />
			<xsd:element name="addResponse" type="tns:addResponseType" />
			<xsd:element name="delete" type="tns:deleteType" />
			<xsd:element name="deleteResponse" type="tns:deleteResponseType" />
			<xsd:element name="list" type="tns:listType" />
			<xsd:element name="listResponse" type="tns:listResponseType" />
			<xsd:element name="login" type="tns:loginType" />
			<xsd:element name="loginResponse" type="tns:loginResponseType" />
			<xsd:complexType name="addType">
				<xsd:sequence>
					<xsd:element name="user" type="tns:user" />
				</xsd:sequence>
			</xsd:complexType>
			<xsd:complexType name="addResponseType">
				<xsd:sequence />
			</xsd:complexType>
			<xsd:complexType name="deleteType">
				<xsd:sequence>
					<xsd:element name="username" type="xsd:string" />
				</xsd:sequence>
			</xsd:complexType>
			<xsd:complexType name="deleteResponseType">
				<xsd:sequence />
			</xsd:complexType>
			<xsd:complexType name="listType">
				<xsd:sequence />
			</xsd:complexType>
			<xsd:complexType name="listResponseType">
				<xsd:sequence minOccurs="1" maxOccurs="unbounded">
					<xsd:element name="user" type="tns:user" />
				</xsd:sequence>
			</xsd:complexType>
			<xsd:complexType name="loginType">
				<xsd:sequence>
					<xsd:element name="username" type="xsd:string" />
					<xsd:element name="passwrod" type="xsd:string" />
				</xsd:sequence>
			</xsd:complexType>
			<xsd:complexType name="loginResponseType">
				<xsd:sequence>
					<xsd:element name="user" type="tns:user" />
				</xsd:sequence>
			</xsd:complexType>
			<xsd:complexType name="user">
				<xsd:sequence>
					<xsd:element name="username" type="xsd:string" />
					<xsd:element name="nickname" type="xsd:string" />
					<xsd:element name="passwrod" type="xsd:string" />
				</xsd:sequence>
			</xsd:complexType>
		</xsd:schema>
	</wsdl:types>

	<wsdl:message name="add">
		<wsdl:part element="tns:add" name="parameters" />
	</wsdl:message>
	<wsdl:message name="addResponse">
		<wsdl:part element="tns:addResponse" name="parameters" />
	</wsdl:message>
	<wsdl:message name="delete">
		<wsdl:part element="tns:delete" name="parameters" />
	</wsdl:message>
	<wsdl:message name="deleteResponse">
		<wsdl:part element="tns:deleteResponse" name="parameters" />
	</wsdl:message>
	<wsdl:message name="login">
		<wsdl:part element="tns:login" name="parameters" />
	</wsdl:message>
	<wsdl:message name="loginResponse">
		<wsdl:part element="tns:loginResponse" name="parameters" />
	</wsdl:message>
	<wsdl:message name="list">
		<wsdl:part element="tns:list" name="parameters" />
	</wsdl:message>
	<wsdl:message name="listResponse">
		<wsdl:part element="tns:listResponse" name="parameters" />
	</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="delete">
			<wsdl:input message="tns:delete" />
			<wsdl:output message="tns:deleteResponse" />
		</wsdl:operation>
		<wsdl:operation name="login">
			<wsdl:input message="tns:login" />
			<wsdl:output message="tns:loginResponse" />
		</wsdl:operation>
		<wsdl:operation name="list">
			<wsdl:input message="tns:list" />
			<wsdl:output message="tns:listResponse" />
		</wsdl:operation>
	</wsdl:portType>

	<wsdl:binding name="userServiceSOAP" 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="delete">
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="login">
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="list">
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:service name="UserService">
		<wsdl:port binding="tns:userServiceSOAP" name="UserServicePort">
			<soap:address location="http://localhost:8800/us/" />
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>


?

使用wsimport导出Java文件,拷贝到客户端中。

基本构架完成。

(编辑:李大同)

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

    推荐文章
      热点阅读