开发流程:
================================
一.先写schema或者wsdl文件
1.在src下创建META-INF/wsdl文件夹
2.新建wsdl并编写文件
(1)编写type
(2)编写message
(3)编写portType:指定接口和方法
(4)编写binding,指定编码样式
(5)编写service
mywsdl.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="c" type="xsd:int" /> <xsd:element name="d" 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: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:message> <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" /> <!-- 添加头信息 --> <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> <!-- 这个name必须和本文件的命名空间名称一致 --> <wsdl:service name="MyServiceImplService"> <wsdl:port binding="tns:myServiceSOAP" name="MyServiceImplPort"> <soap:address location="http://localhost:9999/ms" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
二. 使用wsimport,根据这个文件生成接口(IMyService.java)
IMyService.java
package org.example.mywsdl; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.ws.RequestWrapper; import javax.xml.ws.ResponseWrapper; /** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.1.6 in JDK 6 * Generated source version: 2.1 * */ @WebService(name = "IMyService", targetNamespace = "http://www.example.org/mywsdl/") public interface IMyService { /** * * @param b * @param a * @return * returns int */ @WebMethod @WebResult(name = "addResult", targetNamespace = "") @RequestWrapper(localName = "add", targetNamespace = "http://www.example.org/mywsdl/", className = "org.example.mywsdl.Add") @ResponseWrapper(localName = "addResponse", targetNamespace = "http://www.example.org/mywsdl/", className = "org.example.mywsdl.AddResponse") public int add( @WebParam(name = "a", targetNamespace = "") int a, @WebParam(name = "b", targetNamespace = "") int b, @WebParam(name = "licenseInfo", header = true) String licenseInfo); /** * * @param d * @param c * @return * returns int */ @WebMethod @WebResult(name = "divideResult", targetNamespace = "") @RequestWrapper(localName = "divide", targetNamespace = "http://www.example.org/mywsdl/", className = "org.example.mywsdl.Divide") @ResponseWrapper(localName = "divideResponse", targetNamespace = "http://www.example.org/mywsdl/", className = "org.example.mywsdl.DivideResponse") public int divide( @WebParam(name = "c", targetNamespace = "") int c, @WebParam(name = "d", targetNamespace = "") int d); }
三.编写实现类实现接口(在实现类上指定wsdlLocation)
MyServiceImpl.java
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 { @Override public int add(int a, int b, String licenseInfo) { System.out.println(licenseInfo); System.out.println(a + b); return a + b; } @Override public int divide(int c, int d) { System.out.println(c / d); return c / d; } }
四.添加头信息;
mywsdl.wsdl和IMyService.java文件中已经添加
五.发布服务
MyService.java
package org.example.mywsdl; import javax.xml.ws.Endpoint; public class MyService { public static void main(String[] args) { Endpoint.publish("http://localhost:9999/ms", new MyServiceImpl()); } }
六.使用wsimport,根据发布的服务生成客户端代码
七.创建客户端
新建一个java项目,把生成的所有java文件拷贝到src下面,新建并编写客户端代码
MyClient.java
package org.example.mywsdl; import java.io.IOException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPBodyElement; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPHeader; import javax.xml.soap.SOAPMessage; import javax.xml.ws.Dispatch; import javax.xml.ws.Service; import org.w3c.dom.Document; public class MyClient { public static void main(String[] args) { try { // 创建服务 String ns = "http://www.example.org/mywsdl/"; URL url = new URL("http://localhost:9999/ms"); QName qname = new QName(ns, "MyServiceImplService"); Service service = Service.create(url, qname); // 创建Dispatch QName pname = new QName(ns, "MyServiceImplPort"); Dispatch<SOAPMessage> dispatch = service.createDispatch(pname, SOAPMessage.class, Service.Mode.MESSAGE); // 创建SOAP的body消息 SOAPMessage message = MessageFactory.newInstance().createMessage(); SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); SOAPHeader header = envelope.getHeader(); if (header == null) { header = envelope.addHeader(); } SOAPBody body = envelope.getBody(); QName hname = new QName(ns, "licenseInfo", "ns"); header.addHeaderElement(hname).setValue("123456789"); QName bname = new QName(ns, "add", "ns"); SOAPBodyElement element = body.addBodyElement(bname); element.addChildElement("a").setValue("12"); element.addChildElement("b").setValue("13"); message.writeTo(System.out); System.out.println("\n invoking..."); // 通过dispatch传递消息,返回响应消息 SOAPMessage response = dispatch.invoke(message); response.writeTo(System.out); System.out.println(); // 将响应的消息转换为dom对象 Document doc = response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument(); System.out.println(doc.getElementsByTagName("addResult").item(0).getTextContent()); } catch (SOAPException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
运行该程序
客户端打印结果:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header><ns:licenseInfo xmlns:ns="http://www.example.org/mywsdl/">123456789</ns:licenseInfo></SOAP-ENV:Header><SOAP-ENV:Body><ns:add xmlns:ns="http://www.example.org/mywsdl/"><a>12</a><b>13</b></ns:add></SOAP-ENV:Body></SOAP-ENV:Envelope>
invoking...
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Header/><S:Body><ns2:addResponse xmlns:ns2="http://www.example.org/mywsdl/"><addResult>25</addResult></ns2:addResponse></S:Body></S:Envelope>
25
服务端打印结果:
123456789
25
从打印结果可以看到,发送的消息中包含头信息,并且头信息发送到了服务端
相关推荐
基于契约优先的soap+wsdl开发小功能
基于契约优先的soap开发管理小功能,wsdl实例
WebServicesContractFirstSOAP 我通过 SOAP 的契约优先 Web 服务示例 创建 XSD 创建 WSDL 运行“mvn generate:sources”(将生成对象) 创建客户端 运行“mvn码头:运行”
Java 契约第一个 Web 服务示例 (SOAP) 安装指南 安装 Java jdk 1.7 安装 Maven 3.x 设置环境变量 JAVA_HOME 设置环境变量 MAVEN_HOME 将 JAVA_HOME\bin 和 MAVEN_HOME\bin 放在环境变量 Path 上 克隆应用:git ...
wsdl2java源码 WCFExtrasPlus WCFExtrasPlus 是一组有用的 WCF 扩展: SOAP 标头支持 支持将源代码 XML 注释集成到 WSDL 文档中 覆盖 SOAP 地址位置 URL 单一、扁平化的 WSDL 文件,以更好地与旧 SOAP 工具兼容 作为...
《Java Web 服务:构建与运行(影印...· 理解Web服务描述语言(WSDL)服务契约 · 认识SOAP消息的结构 · 学习如何交付基于Java的RESTful Web服务和消耗商业RESTful服务 · 了解对基于SOAP和基于REST的Web服务的安全要求
ezService主要定位在以下四个方面:简化中小型分布式关系型计算服务开发、规范服务管理、创建完好设计契约、加速服务客户程序开发。集快速服务设计、服务即时发布、安全管理、跨系统协作于一身,是中小型分布式关系...
· 理解Web服务描述语言(WSDL)服务契约 · 认识SOAP消息的结构 · 学习如何交付基于Java的RESTful Web服务和消耗商业RESTful服务 · 了解对基于SOAP和基于REST的Web服务的安全要求 · 学习如何在各种...
之所以要做Java Object与XML的映射,是因为最终作为方法参数和返回值的Java Object要通过网络传输协议(一般是SOAP)传送,这就要求必须对Java Object做类似序列化和反序列化的工作,在SOAP中就是要用XML来表示Java ...