服务器端:

1、编写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:add"/>
<xsd:element name="addResponse" type="tns:addResponse"/>
<xsd:element name="divide" type="tns:divide"/>
<xsd:element name="divideResponse" type="tns:divideResponse"/>
<xsd:element name="licenseInfo" type="xsd:string"/> <xsd:complexType name="add">
<xsd:sequence>
<xsd:element name="a" type="xsd:int"/>
<xsd:element name="b" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="addResponse">
<xsd:sequence>
<xsd:element name="addResult" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType> <xsd:complexType name="divide">
<xsd:sequence>
<xsd:element name="num1" type="xsd:int"/>
<xsd:element name="num2" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="divideResponse">
<xsd:sequence>
<xsd:element name="divideResult" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType> </xsd:schema>
</wsdl:types> <!-- 定义消息 -->
<wsdl:message name="add">
<wsdl:part name="add" element="tns:add"/>
</wsdl:message>
<wsdl:message name="addResponse">
<wsdl:part name="addResponse" element="tns:addResponse"></wsdl:part>
</wsdl:message>
<wsdl:message name="divide">
<wsdl:part name="divide" element="tns:divide"/>
</wsdl:message>
<wsdl:message name="divideResponse">
<wsdl:part name="divideResponse" element="tns:divideResponse"/>
</wsdl:message> <!-- 头消息 -->
<wsdl:message name="licenseInfo">
<wsdl:part name="licenseInfo" element="tns:licenseInfo"></wsdl:part>
</wsdl:message> <!-- 定义port -->
<wsdl:portType name="IMyService">
<wsdl:operation name="add">
<wsdl:input message="tns:add"/>
<wsdl:output message="tns:addResponse"/>
</wsdl:operation> <wsdl:operation name="divide">
<wsdl:input message="tns:divide"/>
<wsdl:output message="tns:divideResponse"/>
</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"/>
<!-- 为add添加隐式的头信息 -->
<soap:header use="literal" part="licenseInfo" message="tns:licenseInfo"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation> <wsdl:operation name="divide">
<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:definitions里的name一致 -->
<wsdl:port binding="tns:myServiceSOAP" name="MyServiceImplPort">
<soap:address location="http://localhost:8989/ms"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

2、使用wsimport命令生成服务端接口类(IMyService.java)
备注:生成的其他文件都可以删除

在add方法的参数定义上加上

@WebParam(name="licenseInfo")String licenseInfo

3、编写服务接口的实现类MyServiceImpl.java

package org.example.mywsdl;

import javax.jws.WebService;

//这里必须制定wsdlLocation来约定参数等名称等
//endpointInterface可写也可不写,写它主要是用到自动生成的一些注解信息
@WebService(endpointInterface="org.example.mywsdl.IMyService",
targetNamespace="http://www.example.org/mywsdl/",
wsdlLocation="META-INF/wsdl/mywsdl.wsdl",
name="MyServiceImpl"
)
public class MyServiceImpl implements IMyService { @Override
public int add(int a, int b, String licenseInfo) {
return a+b;
} @Override
public int divide(int num1, int num2) {
return num1/num2;
} }

4、服务启动类

package org.example.mywsdl;

import javax.xml.ws.Endpoint;

public class RunService {

    public static void main(String[] args) {
//这里的发布地址必须和wsdl中定义的一样
Endpoint.publish("http://localhost:8989/ms", new MyServiceImpl());
}
}

访问服务:http://localhost:8989/ms?wsdl

客户端直接用上面的地址生成即可,生成的wsdl文件即是我们编写的那个wsdl文件

<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --><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:add"></xsd:element>
<xsd:element name="addResponse" type="tns:addResponse"></xsd:element>
<xsd:element name="divide" type="tns:divide"></xsd:element>
<xsd:element name="divideResponse" type="tns:divideResponse"></xsd:element>
<xsd:element name="licenseInfo" type="xsd:string"></xsd:element> <xsd:complexType name="add">
<xsd:sequence>
<xsd:element name="a" type="xsd:int"></xsd:element>
<xsd:element name="b" type="xsd:int"></xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="addResponse">
<xsd:sequence>
<xsd:element name="addResult" type="xsd:int"></xsd:element>
</xsd:sequence>
</xsd:complexType> <xsd:complexType name="divide">
<xsd:sequence>
<xsd:element name="num1" type="xsd:int"></xsd:element>
<xsd:element name="num2" type="xsd:int"></xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="divideResponse">
<xsd:sequence>
<xsd:element name="divideResult" type="xsd:int"></xsd:element>
</xsd:sequence>
</xsd:complexType> </xsd:schema>
</wsdl:types> <!-- 定义消息 -->
<wsdl:message name="add">
<wsdl:part name="add" element="tns:add"></wsdl:part>
</wsdl:message>
<wsdl:message name="addResponse">
<wsdl:part name="addResponse" element="tns:addResponse"></wsdl:part>
</wsdl:message>
<wsdl:message name="divide">
<wsdl:part name="divide" element="tns:divide"></wsdl:part>
</wsdl:message>
<wsdl:message name="divideResponse">
<wsdl:part name="divideResponse" element="tns:divideResponse"></wsdl:part>
</wsdl:message> <!-- 头消息 -->
<wsdl:message name="licenseInfo">
<wsdl:part name="licenseInfo" element="tns:licenseInfo"></wsdl:part>
</wsdl:message> <!-- 定义port -->
<wsdl:portType name="IMyService">
<wsdl:operation name="add">
<wsdl:input message="tns:add"></wsdl:input>
<wsdl:output message="tns:addResponse"></wsdl:output>
</wsdl:operation> <wsdl:operation name="divide">
<wsdl:input message="tns:divide"></wsdl:input>
<wsdl:output message="tns:divideResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType> <!-- 绑定服务 -->
<wsdl:binding name="myServiceSOAP" type="tns:IMyService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"></soap:binding>
<wsdl:operation name="add">
<wsdl:input>
<soap:body use="literal"></soap:body>
<!-- 为add添加隐式的头信息 -->
<soap:header use="literal" part="licenseInfo" message="tns:licenseInfo"></soap:header>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"></soap:body>
</wsdl:output>
</wsdl:operation> <wsdl:operation name="divide">
<wsdl:input>
<soap:body use="literal"></soap:body>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"></soap:body>
</wsdl:output>
</wsdl:operation>
</wsdl:binding> <!-- 发布服务 -->
<wsdl:service name="MyServiceImplService"><!-- 这里的名字必须和<wsdl:definitions里的name一致 -->
<wsdl:port binding="tns:myServiceSOAP" name="MyServiceImplPort">
<soap:address location="http://localhost:8989/ms"></soap:address>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

使用该wsdl生成客户端java代码后的add方法如下:

@WebMethod
@WebResult(name = "addResult", targetNamespace = "")
@RequestWrapper(localName = "add", targetNamespace = "http://www.example.org/mywsdl/", className = "com.ws04.Add")
@ResponseWrapper(localName = "addResponse", targetNamespace = "http://www.example.org/mywsdl/", className = "com.ws04.AddResponse")
public int add(@WebParam(name = "a", targetNamespace = "") int a,
@WebParam(name = "b", targetNamespace = "") int b);

此时方法的参数里没有看到有头信息参数的定义,调用该方法可以使用handler或者soap来编码传递头信息

webservice系统学习笔记9-使用契约优先的方式的一个服务端demo(隐式传Header信息)的更多相关文章

  1. webservice系统学习笔记10-使用jax-ws创建基于tomcat类型的容器的ws服务

    1.在web-info目录下新建目录wsdl 2.在1步的目录下 新建文件user.wsdl <?xml version="1.0" encoding="UTF-8 ...

  2. webservice系统学习笔记7-使用handler实现过滤器/拦截器效果

    handler可以作用于客户端,也可以作用了服务端 handler分为:1.LogicalHandler:只能获取到soap消息的body. 2.SOAPHandler:可以获取SOAPMessage ...

  3. webservice系统学习笔记5-手动构建/发送/解析SOAP消息

    手动拼接SOAP消息调用webservice SOAP消息的组成: 1.创建需要发送的SOAP消息的XML(add方法为例子) /** * 创建访问add方法的SOAP消息的xml */ @Test ...

  4. webservice系统学习笔记4-使用工具查看SOAP消息

    使用myeclipse的WTP java ee视图里的[web services Explorer]来测试查看webservice传输的SOAP消息 1. 2. 测试getUserByUsername ...

  5. webservice系统学习笔记2-使用jdk的命令生成本地代码

    使用jdk自带的命令wsimport生成远程服务的本地代码 C:\Documents and Settings\Administrator>wsimport -d E:\mhWorkspace\ ...

  6. webservice系统学习笔记1-使用注解创建ws服务

    简单入门之helloword,具体详细的在后面的章节详细介绍. 使用JDK自带的jax-ws创建并发布一个简单的webservice 在本地创建服务,然后本机访问 1.创建服务提供接口 IMyServ ...

  7. webservice系统学习笔记8-简单的权限校验

    服务端handler.java package com.ws01; import java.util.Set; import javax.xml.namespace.QName; import jav ...

  8. webservice系统学习笔记7-异常处理

    接口类:IMyService.java @WebResult(name="testExceptionResult") public void testException() thr ...

  9. webservice系统学习笔记6-使用soap的header传递消息

    1.显示的使用soap的header传递消息(不推荐使用,会破坏正常的代码结构,推荐使用handler处理) @WebResult(name="deleteResult") pub ...

随机推荐

  1. 读书笔记-APUE第三版-(8)进程控制

    进程ID 每一个进程都有一个唯一的进程ID.几个特殊进程: 0号进程是内核进程,一般是调度进程swapper. 1号进程init,是用户进程(以root权限执行/sbin/init),负责初始化. 几 ...

  2. Class.forName(String name)方法,到底会触发那个类加载器进行类加载行为?

    4.2 在代码中直接调用Class.forName(String name)方法,到底会触发那个类加载器进行类加载行为? Class.forName(String name)默认会使用调用类的类加载器 ...

  3. 彻底理解jdbc为什么用反射创建驱动程序对象

    1.class.forName(mysql),这样更换数据库时,不需要更改程序代码,程序不需要重新编译就能运行. 因为反射是动态编译的,程序运行期间生成指定类的对象, 这样就可以程序运行期间生成不同的 ...

  4. IOS开发之----代码块的使用(二)

    iOS4引入了一个新特性,支持代码块的使用,这将从根本上改变你的编程方式.代码块是对C语言的一个扩展,因此在Objective-C中完全支持.如果你学过Ruby,Python或Lisp编程语言,那么你 ...

  5. IOS学习之基于IOS7的tab bar

    转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/28129473 作者:小马 什么是tabbar? 先几张图:      上图中蓝色框 ...

  6. java表格 JTable实例 (带滚动条,内嵌选择框)

    import javax.swing.JTable; import javax.swing.table.AbstractTableModel; import javax.swing.JScrollPa ...

  7. 解决kylin sync table报错:MetaException(message:java.lang.ClassNotFoundException Class org.apache.hive.hcatalog.data.JsonSerDe not found

    在kylin-gui中sync表default.customer_visit时报错: -- ::, ERROR [http-bio--exec-] controller.BasicController ...

  8. @JVM垃圾回收机制的一些概念

    数据类型 Java虚拟机中,数据类型可以分为两类:基本数据类型和引用数据类型 .基本类型的变量保存的值就是数值本身:而引用类型的变量保存引用值."引用值"代表了某个对象的引用,而不 ...

  9. 使用SGD(Stochastic Gradient Descent)进行大规模机器学习

    原贴地址:http://fuliang.iteye.com/blog/1482002  其它参考资料:http://en.wikipedia.org/wiki/Stochastic_gradient_ ...

  10. pymysql的使用心得(1)------小细节,注意!

    最近一段时间开始使用MySQL,使用的是pymysql库. 其中遇到过一些小问题,值得记录一下,以便今后使用的时候注意到. 表格的建立,代码如下: cursor.execute("creat ...