标签: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(); } } }
标签:wso2
原文地址:http://blog.csdn.net/szh1124/article/details/44061625