基于契约优先的隐式头信息处理


<?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/my/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
name="myServiceImplService" targetNamespace="http://www.example.org/my/">
<!-- TYPE -->
<wsdl:types>
<!-- 定义schema,通过tns引用该schema中的元素 -->
<xsd:schema targetNamespace="http://www.example.org/my/">
<xsd:element name="add" type="tns:addType"/>
<xsd:element name="addResponse" type="tns:addResponseType"/>
<!-- 为license创建element元素 -->
<xsd:element name="license" type="xsd:string"/>

<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:schema>
</wsdl:types>

<!-- MESSAGE -->
<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>
<!-- 为license创建新的message -->
<wsdl:message name="license">
<wsdl:part name="license" element="tns:license"></wsdl:part>
</wsdl:message>

<!-- PORTTYPE 指定接口和方法 -->
<wsdl:portType name="IMyservice">
<wsdl:operation name="add">
<wsdl:input message="tns:add"/>
<wsdl:output message="tns:addResponse"/>
</wsdl:operation>
</wsdl:portType>

<!-- BINDING 指定编码方式 -->
<wsdl:binding name="MyServiceImplPortBinding" type="tns:IMyservice">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="add">
<wsdl:input>
<!-- add方法的header中加入license消息 -->
<soap:header use="literal" part="license" message="tns:license"></soap:header>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>

<!--
SERVICE服务
注意:service name必须与wsdl definition中的name一致
-->
<wsdl:service name="MyServiceImplService">
<wsdl:port binding="tns:MyServiceImplPortBinding" name="MyServiceImplPort">
<!-- 指定服务发布的地址 -->
<soap:address location="http://localhost:7777/ms"/>
</wsdl:port>
</wsdl:service>

</wsdl:definitions>





package org.example.my;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
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/my/")
public interface IMyservice {


/**
*
* @param b
* @param a
* @param license 头信息
* @return
* returns int
*/
@WebMethod
@WebResult(name = "addResult", targetNamespace = "")
@RequestWrapper(localName = "add", targetNamespace = "http://www.example.org/my/", className = "org.example.my.AddType")
@ResponseWrapper(localName = "addResponse", targetNamespace = "http://www.example.org/my/", className = "org.example.my.AddResponseType")
public int add(
@WebParam(name = "a", targetNamespace = "")
int a,
@WebParam(name = "b", targetNamespace = "")
int b,
@WebParam(name = "license", header=true)
String license);

}




package org.example.my;

import javax.jws.WebService;
/**
* 实现类
* 指定wsdlLocation="META-INF/wsdl/my.wsdl",使用本地以及编写好的wsdl文件
*/
@WebService(endpointInterface="org.example.my.IMyservice",
targetNamespace = "http://www.example.org/my/",
wsdlLocation="META-INF/wsdl/my.wsdl")
public class MyServiceImpl implements IMyservice {

@Override
public int add(int a, int b, String license) {
//如果客户端没有传递头信息,则license为null
System.out.println("MyServiceImpl.add() "+license);
return a+b;
}

}


[b]开启服务[/b]

package org.example.my;

import javax.xml.ws.Endpoint;

public class MyServer {
public static void main(String[] args) {
String address = "http://localhost:7777/ms";
IMyservice implementor = new MyServiceImpl();
Endpoint.publish(address, implementor);
}

}



[b]基于契约优先的方式,wsdl中定义的头信息,在客户端生成的代码中不体现[/b]



package org.example.my;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
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/my/")
@XmlSeeAlso({
ObjectFactory.class
})
public interface IMyservice {


/**
*
* @param b
* @param a
* @return
* returns int
*/
@WebMethod
@WebResult(name = "addResult", targetNamespace = "")
@RequestWrapper(localName = "add", targetNamespace = "http://www.example.org/my/", className = "org.example.my.AddType")
@ResponseWrapper(localName = "addResponse", targetNamespace = "http://www.example.org/my/", className = "org.example.my.AddResponseType")
public int add(
@WebParam(name = "a", targetNamespace = "")
int a,
@WebParam(name = "b", targetNamespace = "")
int b);

}



[b]客户端调用服务端的服务[/b]

package org.example.my.test;

import static org.junit.Assert.*;

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.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;

import org.example.my.IMyservice;
import org.example.my.MyServiceImplService;


public class Test {
/**
* 使用本地接口调用服务端的服务
*/
@org.junit.Test
public void testNoHeader() {
MyServiceImplService serviceImpl = new MyServiceImplService();
IMyservice service = serviceImpl.getMyServiceImplPort();
int result = service.add(1, 2);
System.out.println(result);
}

/**
* 通过SOAP发送消息
* 并在header中传递隐式的头信息
* 隐式的原因:客户端根据wsdl生成的代码中,接口方法中的参数并没有增加
* 客户端可以在header中传递信息,服务端可以解析出来
*/
@org.junit.Test
public void testHeader() throws Exception {
String ns = "http://www.example.org/my/";
String localPart = "MyServiceImplService";
String address = "http://localhost:7777/ms";
String prefix = "ns";

URL wsdlDocumentLocation = new URL(address);
QName serviceName = new QName(ns,localPart,prefix);
//创建服务
Service service = Service.create(wsdlDocumentLocation,serviceName);

String prot = "MyServiceImplPort";
QName portName = new QName(ns, prot);
//创建dispatcher
Dispatch<SOAPMessage> dispatcher =
service.createDispatch(portName,SOAPMessage.class,Service.Mode.MESSAGE);

SOAPMessage message = MessageFactory.newInstance().createMessage();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
SOAPHeader header = envelope.getHeader();
SOAPBody body = envelope.getBody();
if(header==null)
header = envelope.addHeader();

//添加header
String hearderLoaclPart = "license";
QName headerQName = new QName(ns, hearderLoaclPart, prefix);
SOAPHeaderElement headerEle = header.addHeaderElement(headerQName);
headerEle.setValue("this is 隐式头消息");

//添加body
//指名访问的方法名:add
String bodyLoaclPart = "add";
QName bodyQName = new QName(ns, bodyLoaclPart, prefix);
SOAPBodyElement bodyEle = body.addBodyElement(bodyQName);
bodyEle.addChildElement("a").setValue("99");
bodyEle.addChildElement("b").setValue("101");

message.writeTo(System.out);
System.out.println("\n invoking...");

SOAPMessage retMsg = dispatcher.invoke(message);
retMsg.writeTo(System.out);

}
}


[b]运行结果[/b]
[color=blue]testNoHeader()
客户端:3
服务端:MyServiceImpl.add() null[/color]

testHeader()
客户端:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<ns:license xmlns:ns="http://www.example.org/my/">this is 隐式头消息</ns:license>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns:add xmlns:ns="http://www.example.org/my/">
<a>99</a>
<b>101</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/my/">
<addResult>200</addResult>
</ns2:addResponse>
</S:Body>
</S:Envelope>

服务端:
MyServiceImpl.add() this is 隐式头消息
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值