使用CXF实现带header的soap服务

接口部分使用如下代码

package ws.server;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebParam.Mode;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.Holder;

import ws.bean.MyReq;
import ws.bean.MyResp;

@WebService(targetNamespace = "http://www.MyWs.com/schemas/")
public interface MyWs {
	@WebMethod
	@WebResult(name = "MyResp") MyResp
	myMethod(
		//header说明这个参数出现在标头部分,Mode.INOUT说明这个参数是输入输出都存在的(注意类型要用Holder包围)
		@WebParam(name = "Tid", header = true, mode = Mode.INOUT) Holder<String> tid,
		@WebParam(name = "MyReq") MyReq myReq
	);
}

 

实现部分的代码

package ws.server.impl;

import javax.xml.ws.Holder;

import ws.bean.MyReq;
import ws.bean.MyResp;
import ws.server.MyWs;

public class MyWsImpl implements MyWs {
	@Override
	public MyResp myMethod(Holder<String> tid, MyReq myReq) {
		//使用value获取Holder中实际类型的值
		System.out.println("tid = " + tid.value); 
		return new MyResp();
	}
}

 使用soapUI工具可以看到输入

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://www.MyWs.com/schemas/">
   <soapenv:Header>
      <sch:Tid>2222</sch:Tid>
   </soapenv:Header>
   <soapenv:Body>
      <sch:myMethod>
         <MyReq>
            <reqName>3333</reqName>
         </MyReq>
      </sch:myMethod>
   </soapenv:Body>
</soapenv:Envelope>

 

以及返回的输出,都带有header部分
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Header>
      <ns2:Tid xmlns:ns2="http://www.MyWs.com/schemas/">2222</ns2:Tid>
   </soap:Header>
   <soap:Body>
      <ns2:myMethodResponse xmlns:ns2="http://www.MyWs.com/schemas/">
         <MyResp>
            <respName>javax.xml.ws.Holder@1733fe5d,3333</respName>
         </MyResp>
      </ns2:myMethodResponse>
   </soap:Body>
</soap:Envelope>
 

你可能感兴趣的:(java,CXF,WebServices)