axis1 axis2 使用总结

-----------------------------------------axis1.4-------------------------------------------------

1. 生成java对象
   1.1 设置环境变量

AXIS_HOME=D:\OpenSource\axis\axis-1_4
AXIS_LIB=%AXIS_HOME%\lib
AXISCLASSPATH=%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;%AXIS_LIB%\log4j-1.2.8.jar;%AXIS_LIB%\wsdl4j-1.5.1.jar;%AXIS_LIB%\xml-apis.jar;%AXIS_LIB%\xercesImpl.jar
CLASSPATH=%AXISCLASSPATH%;.

   1.2 执行命令行

java org.apache.axis.wsdl.WSDL2Java ***/services/***?wsdl

2. web service 调用

public class TermSvrUtil {
	
	private static ThreadLocal<TermSvrSoapBindingStub> stub = new ThreadLocal<TermSvrSoapBindingStub>();
	
	/**
	 * 用户验证
	 * @param lt
	 * @return 
	 */
	public static LoginResult loginTokenAuth(LoginToken lt) {
		try{
			if (stub.get() == null) {
				stub.set(new TermSvrSoapBindingStub(new URL("url地址"), null));
			}
			logger.debug("loginTokenAuth",  StringUtil.toString(lt));  //..
			TermSvrSoapBindingStub termSvrSoapBindingStub = stub.get();
			LoginResult lr = termSvrSoapBindingStub.loginTokenAuth(lt);  //调用方法
			logger.debug("loginTokenAuth ",  "response:" + StringUtil.toString(lr));
			return lr;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("loginTokenAuth", "token :(" + lt + ") ", e);
			return null;
		}
		
	}

另一篇推荐:http://hi.baidu.com/sbzudacwaubmrxd/item/388514d83fb53453d73aae7c

---------------------------------------------axis2---------------------------

1.生成java对象

WSDL2Java  -uri http://*****/services/***?wsdl

2. 调用

public class Axis2ClientUtil
{
	static public void initStub(Stub stub, String user, String password) throws XMLStreamException, AxisFault
	{
		ServiceClient client = stub._getServiceClient();
		Options options = client.getOptions();
		//Load Policy
		InputStream resource = new ByteArrayInputStream(POLICY.getBytes());
		StAXOMBuilder builder = new StAXOMBuilder(resource);
		Policy policy = PolicyEngine.getPolicy(builder.getDocumentElement());
		options.setProperty(RampartMessageData.KEY_RAMPART_POLICY, policy);
		options.setUserName(user);
		options.setPassword(password);
		client.engageModule("rampart");

	}
	static private String POLICY ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
			"<wsp:Policy wsu:Id=\"UsernameToken\" xmlns:wsu=\n" +
			"    \"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"\n" +
			"    xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\">\n" +
			"  <wsp:ExactlyOne>\n" +
			"    <wsp:All>\n" +
			"      <sp:SupportingTokens\n" +
			"          xmlns:sp=\"http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702\">\n" +
			"        <wsp:Policy>\n" +
			"          <sp:UsernameToken sp:IncludeToken=\n" +
			"              \"http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702/IncludeToken/AlwaysToRecipient\">\n" +
			"            <wsp:Policy>\n" +
			"              <sp:HashPassword/>\n" +
			"            </wsp:Policy>\n" +
			"          </sp:UsernameToken>\n" +
			"        </wsp:Policy>\n" +
			"      </sp:SupportingTokens>\n" +
			"    </wsp:All>\n" +
			"  </wsp:ExactlyOne>\n" +
			"</wsp:Policy>";

}

public class BalanceServiceAxis2Util {
    private static Logger logger = Logger.getLogger(BalanceServiceAxis2Util.class);
    private static ThreadLocal<BalanceServiceStub>  stub = new ThreadLocal<BalanceServiceStub>();
    
    /**
     *  余额查询
     * @param acctId
     * @return
     * @throws Exception
     */
    public static long queryBalance(long acctId) throws Exception {
        try  {
            if(stub.get()==null)    {
                stub.set( new BalanceServiceStub( AbstractWebServiceUtil.getUrl().trim() + "/BalanceService"));
                Axis2ClientUtil.initStub(stub.get(), AbstractWebServiceUtil.getUser(), AbstractWebServiceUtil.getPassword());
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        BalanceServiceStub.QueryBalance req = new BalanceServiceStub.QueryBalance();
        req.setAcctID(acctId);
        BalanceServiceStub.QueryBalanceResponse resp = stub.get().queryBalance(req);
        long result = resp.get_return();
        return result;
    }

你可能感兴趣的:(axis1 axis2 使用总结)