码迷,mamicode.com
首页 > Windows程序 > 详细

(11)wso2esb 服务 API调用

时间:2015-03-04 19:17:29      阅读:357      评论:0      收藏:0      [点我收藏+]

标签:wso2

这篇文章介绍如何通过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();
		}
	}
}



(11)wso2esb 服务 API调用

标签:wso2

原文地址:http://blog.csdn.net/szh1124/article/details/44061625

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!