当前位置:首页 > 开发 > 编程语言 > Web Service > 正文

使用cxf实现webService,客户端填写信息到soap header

发表于: 2014-04-22   作者:黑龙0110   来源:转载   浏览:
摘要: 最近调试公司的webService,我用java实现客户端。在调试中遇到点困难,客户端调用服务端需要先签证,签证正确后需要将签证返回的结果保存到soap header中用于请求验证。网上找了好多,参考并不断尝试,最终搞定了。今天分享出来,希望能对有需要的朋友有帮助。我这直接就粘代码了 package com.hzsun.client; import java.util.ArrayL
最近调试公司的webService,我用java实现客户端。在调试中遇到点困难,客户端调用服务端需要先签证,签证正确后需要将签证返回的结果保存到soap header中用于请求验证。网上找了好多,参考并不断尝试,最终搞定了。今天分享出来,希望能对有需要的朋友有帮助。我这直接就粘代码了
package com.hzsun.client;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.binding.soap.interceptor.SoapPreProtocolOutInterceptor;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.jaxb.JAXBDataBinding;
import org.apache.cxf.phase.Phase;

import com.hzsun.SecurityHeader;

public class AddSoapSecurityHeader extends AbstractSoapInterceptor {

	/**
	 * 
	 * @Title:在发送消息前,封装Soap Header 信息
	 * 
	 * @Description:
	 * 
	 * @Copyright:
	 * 
	 * @author zz
	 * @version 1.00.000
	 * 
	 */

	public AddSoapSecurityHeader() {
		super(Phase.WRITE);
		addAfter(SoapPreProtocolOutInterceptor.class.getName());

	}

	private SecurityHeader securityHeader;

	public void handleMessage(SoapMessage message) throws Fault {
		System.out.println("AddSoapSecurityHeader handleMessage invoked");

		List<Header> headers = new ArrayList<Header>();
		try {
			Header dummyHeader = new Header(new QName("http://www.hzsun.com/",
					"SecurityHeader"), securityHeader, new JAXBDataBinding(
					SecurityHeader.class));
			headers.add(dummyHeader);
			message.put(Header.HEADER_LIST, headers);
		} catch (JAXBException e) {
			e.printStackTrace();
		}

	}

	public SecurityHeader getSecurityHeader() {
		return securityHeader;
	}

	public void setSecurityHeader(SecurityHeader securityHeader) {
		this.securityHeader = securityHeader;
	}

}



客户端调用

import javax.xml.namespace.QName;

import org.apache.cxf.frontend.ClientProxy;

import com.hzsun.SecurityHeader;
import com.hzsun.ThirdWebservice;
import com.hzsun.ThirdWebserviceSoap;
import com.hzsun.client.AddSoapSecurityHeader;

public class Client {
	private static final QName SERVICE_NAME = new QName(
			"http://www.hzsun.com/", "ThirdWebservice");

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ThirdWebservice ss = new ThirdWebservice(ThirdWebservice.WSDL_LOCATION,
				SERVICE_NAME);
		ThirdWebserviceSoap port = ss.getThirdWebserviceSoap();

		System.out.println("Invoking signIn...");
		javax.xml.ws.Holder<java.lang.String> _signIn_signInResult = new javax.xml.ws.Holder<java.lang.String>();
		javax.xml.ws.Holder<java.lang.Integer> _signIn_stanum = new javax.xml.ws.Holder<java.lang.Integer>();
		javax.xml.ws.Holder<java.lang.Integer> _signIn_thirdType = new javax.xml.ws.Holder<java.lang.Integer>();
		javax.xml.ws.Holder<java.lang.Integer> _signIn_secret1 = new javax.xml.ws.Holder<java.lang.Integer>();
		javax.xml.ws.Holder<java.lang.String> _signIn_secret2 = new javax.xml.ws.Holder<java.lang.String>();
		port.signIn(_signIn_signInResult, _signIn_stanum, _signIn_thirdType,
				_signIn_secret1, _signIn_secret2);

		System.out.println("signIn._signIn_signInResult="
				+ _signIn_signInResult.value);
		System.out.println("signIn._signIn_stanum=" + _signIn_stanum.value);
		System.out.println("signIn._signIn_thirdType="
				+ _signIn_thirdType.value);
		System.out.println("signIn._signIn_secret1=" + _signIn_secret1.value);
		System.out.println("signIn._signIn_secret2=" + _signIn_secret2.value);

		SecurityHeader securityHeader = new SecurityHeader();
		securityHeader.setSecret1(_signIn_secret1.value);
		securityHeader.setSecret2(_signIn_secret2.value);
		securityHeader.setThirdType(_signIn_thirdType.value);
//在这调用
		AddSoapSecurityHeader soapSecurityHeader = new AddSoapSecurityHeader();
//传了一个对象参数,有服务端决定
		soapSecurityHeader.setSecurityHeader(securityHeader);
		org.apache.cxf.endpoint.Client cxfClient = ClientProxy.getClient(port);
		cxfClient.getOutInterceptors().add(soapSecurityHeader);

		
	}

}

使用cxf实现webService,客户端填写信息到soap header

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号