wso2——(11)esb 服务 API调用

这篇文章介绍如何通过API调用部署在WSO2 ESB上的服务。

下面以WSO2 自带服务echo为例进行介绍。

1、生成客户端

点击echo服务名称,出现如下界面。

点击“Generate Axis2 Client”,出现如下界面。

点击“Generate”,生成一个zip文件。

2、生成客户端代码。

解压zip文件,导入Eclipse,或者直接在pox.xml文件目录下输入mvn clean install。生成客户端java代码。

3、客户端认证调用

编写程序调用服务

package com;

import java.io.File;

import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.HttpTransportProperties;
import org.wso2.carbon.core.services.echo.EchoStub;
import org.wso2.carbon.core.services.echo.EchoStub.EchoString;

public class echoClient {
	private static String SEVER_URL = "http://localhost:9763/services/";
	private static String USER_NAME = "admin";
	private static String PASSWORD = "admin";

	public static void main(String args[]) {
		String trustStore = System.getProperty("user.dir") + File.separator + "src"
				+ File.separator + "main" + File.separator + "resources" + File.separator
				+ "wso2carbon.jks";
		System.setProperty("javax.net.ssl.trustStore", trustStore);
		System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
		ConfigurationContext configContext;
		try {
			configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem(
					null, null);

			String serviceEndPoint = SEVER_URL + "echo";
			EchoStub stub = new EchoStub(configContext, serviceEndPoint);
			ServiceClient client = stub._getServiceClient();
			Options option = client.getOptions();
			option.setProperty(HTTPConstants.COOKIE_STRING, null);
			HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();
			auth.setUsername(USER_NAME);
			auth.setPassword(PASSWORD);
			auth.setPreemptiveAuthentication(true);
			option.setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE, auth);
			option.setManageSession(true);

			EchoString echoString = new EchoString();

			echoString.setIn("string");
			System.out.println(stub.echoString(echoString).get_return());
			String authCookie = (String) stub._getServiceClient().getServiceContext()
					.getProperty(HTTPConstants.COOKIE_STRING);

			System.out.println(authCookie);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}



评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值