搜狗首页  |   服务首页  |   客服热线:010-56898080

使用cxf构造客户端

请先下载示例工程(超链接,文件下载)
下面以 AccountService 为例,部署 API 客户端代码
1. 打开 Eclipse ,点击 File Import, 选择 Existing Projects into Workspace, 点击 next. 选择 api-client 所在的路径,如下图所示



2. 点击 finish
3. 右键点击 api-client, 选择 buildpath < configure build path… 如下所示



4. 如果图中jar包不在工程下,您需要点击Add external Jars,导入需要的CXF相关的jar包(以上图中的5个),点击ok
5. 将 AccountService.wsdl ,放在工程目录下。
6. 右键点击 api.client.account 下的 AccountService_AccountService_Client.java 类,选择 Run as Application 。在控制台出现:



表明 API 客户端部署成功。
示例程序如下:
package api.client.account;

import java.net.URL;
import javax.xml.namespace.QName;
import com.sogou.api.sem.v1.account.AccountService;
import com.sogou.api.sem.v1.account.AccountService_Service;
import com.sogou.api.sem.v1.account.GetAccountInfoRequest;
import com.sogou.api.sem.v1.common.AuthHeader;
import com.sogou.api.sem.v1.common.ResHeader;

public final class AccountService_AccountService_Client {
private static final QName SERVICE_NAME = new QName("https://api.sogou.com/sem/sms/v1", "AccountService");

private AccountService_AccountService_Client() {
}

public static void main(String args[]) throws java.lang.Exception {
URL wsdlURL = new URL("file:accountService.wsdl");

AccountService_Service ss = new AccountService_Service(wsdlURL, SERVICE_NAME);
AccountService port = ss.getAccountService();

{
System.out.println("Invoking getAccountInfo...");
com.sogou.api.sem.v1.account.GetAccountInfoRequest _getAccountInfo_parameters = new GetAccountInfoRequest();
AuthHeader _getAccountInfo_authHeader = new AuthHeader();
_getAccountInfo_authHeader.setAgentpassword("22222222");
_getAccountInfo_authHeader.setAgentusername("guojing1@sogou.com");
_getAccountInfo_authHeader.setUsername("guojing233@sogou.com");
_getAccountInfo_authHeader.setPassword("22222222");
_getAccountInfo_authHeader.setToken("token_agt4804");

javax.xml.ws.Holder<ResHeader> _getAccountInfo_resHeader = new javax.xml.ws.Holder<ResHeader>();
com.sogou.api.sem.v1.account.GetAccountInfoResponse _getAccountInfo__return = port.getAccountInfo(_getAccountInfo_parameters, _getAccountInfo_authHeader, _getAccountInfo_resHeader);
System.out.println("getAccountInfo.result=" + _getAccountInfo__return);
System.out.println("getAccountInfo._getAccountInfo_resHeader=" + _getAccountInfo_resHeader.value);
}
}
}