The following macros are not currently supported in the header:
  • style

附录 B Web 服务客户端示例

Service Desk Web Service API 基于 Apache Axis2 项目,该项目是对 Web 服务等式的客户端和服务器端的基于 Java 的实施。 Axis 本质上是一种 SOAP 引擎:用于构造 SOAP 处理器(如客户端、服务器等)的框架。

下面的示例展示了开发客户端代码来利用 Service Desk Web Service 的结构。

本节包含以下主题:                                                                                                        

 

设置环境

下载并解压缩 Apache Axis2 的二进制版本 (axis2-1.5.1-bin.zip)。 将文件解压缩至 E:Axis2Client(假设这是您的客户端目录)之后,您将得到目录“E:Axis2Clientaxis2-1.5.1-binaxis2-1.5.1”。 此目录包含 Apache Axis2 引擎的二进制版本。 现在,通过从命令行调用以下代码来设置环境变量:

@set AXIS2_HOME=D:\ java-tools\ axis2-1.5.1

使用 wsdl2java 工具生成 Axis2 客户端代码

Web 服务说明语言 (WSDL) 文件描述了一种 Web 服务。 适用于基于 XML 的远程过程调用 (JAX-RPC) 1.1 规范的 Java API 定义了与 Web 服务交互的 Java API 映射。

适用于 Java 2 平台企业版 (J2EE) 1.1 规范的 Web 服务定义了在 J2EE 环境中部署 Web 服务的部署描述符。 WSDL2Java 命令针对 WSDL 文件执行,以便根据这些规范建立 Java API 和部署描述符模板。

命令行语法为:

WSDL2Java [arguments] WSDL-URI

通过从命令行执行以下命令来生成 ServiceRequest Web 服务的客户端代码:

%AXIS2_HOME%\ bin\ WSDL2Java -url  http://nsd-preview.nimsoftondemand.com/webservices/ServiceRequest?wsdl -o com/infradeskonline/genclient

其中,

  • “-url”是您以“.wsdl”或“.xml”格式保存 wsdl 副本的位置路径,或者是 WSDL 所在的 URL。
  • “-o”是您希望的文件的输出路径。 如果未指定,文件将输出至当前的 bin 位置。
  • 上述命令将“ServiceRequestStub.java”和“ServiceRequestCallbackHandler.java”生成到“com/infradeskonline/genclient”目录中。

典型 API 调用序列

对于每个调用,您的客户端应用程序通常执行以下任务:

  • 通过定义请求参数来准备请求(如果适用)。
  • 执行调用,将请求及其参数传递至 Service Desk Web Service 进行处理。
  • 接收来自 API 的响应。
  • 处理该响应,即处理返回的数据(如果调用成功)或处理错误(如果调用失败)。

get 调用客户端示例
 

以下代码是 Axis Client 程序示例,该程序调用 getServiceRequest 操作,以 XML 输出格式查询与指定故障单标识符标识的服务请求有关的主要详细信息(一般信息)摘录。

public class ServiceRequestClient
{    


public void main (

String [] args)
   { 


try
      { 
         // Create the request
         ServiceRequestStub                        srqStub         = 

new ServiceRequestStub();
         ServiceRequestStub.Credentials            credentials     = 

new ServiceRequestStub.Credentials();
         ServiceRequestStub.ExtendedSettings       extParams       = 

new ServiceRequestStub.ExtendedSettings();

         // Initialize Credentials (User Name & User Password / Authorization Token & Slice Token)
         credentials.setUserName (

"wsuser");
         credentials.setUserPassword (

"wsuser");

         // Initialize Extended Settings such as Response Format (XML, JSON)
         extParams.setResponseFormat (

"XML");

         // Invoke the GET service
         ServiceRequestStub.DefaultServiceResponse serviceResponse = srqStub.getServiceRequest (credentials, extParams, ticketIdentifier);

         // Inspect successful execution of service and retrieve the response text


if (serviceResponse.getResponseStatus ().equals (

"OK"))
         { 


System.out.println(

"XML Response : " + serviceResponse.getResponseText ());  
         } 
         // Retrieve the status code, status message and error messages, in 

case of failures


else
         { 


System.out.println(

"Status Code : " + serviceResponse.getStatusCode ());


System.out.println(

"Status Message : " + serviceResponse.getStatusMessage ());


System.out.println(

"Errors : " + Arrays.asList (serviceResponse.getErrors ()).toString ());
         } 
      } 


catch (Exception e) 
      { 


System.out.println (

"Exception: " + e.getMessage());
                } 
   } 
} 

list 调用客户端示例

以下代码是采用 JSON 输出格式的 Axis 客户端程序示例,该程序调用 listServiceRequest 操作,以查询并返回登录用户及其组的、匹配指定搜索条件的所有未结服务请求的列表。

public class ServiceRequestClient
{    


public void main (

String [] args)
   { 


try
      { 
         // Create the request
         ServiceRequestStub                        srqStub         = 

new ServiceRequestStub();
         ServiceRequestStub.Credentials            credentials     = 

new ServiceRequestStub.Credentials();
         ServiceRequestStub.ExtendedSettings       extParams       = 

new ServiceRequestStub.ExtendedSettings();

         // Initialize Credentials (User Name & User Password / Authorization Token & Slice Token)
         credentials.setUserName (

"wsuser");
         credentials.setUserPassword (

"wsuser");

         // Initialize Extended Settings such as Response Format (XML, JSON)
         extParams.setResponseFormat (

"JSON");

         // Invoke the LIST service
         ServiceRequestStub.DefaultServiceResponse serviceResponse = srqStub.listServiceRequest (credentials, extParams, ticketIdentifier);

         // Inspect successful execution of service and retrieve the response text


if (serviceResponse.getResponseStatus ().equals (

"OK"))
         { 


System.out.println(

"JSON Response : " + serviceResponse.getResponseText ());  
         } 
         // Retrieve the status code, status message and error messages, in 

case of failures


else
         { 


System.out.println(

"Status Code : " + serviceResponse.getStatusCode ());


System.out.println(

"Status Message : " + serviceResponse.getStatusMessage ());


System.out.println(

"Errors : " + Arrays.asList (serviceResponse.getErrors ()).toString ());
         } 
      } 


catch (Exception e) 
      { 


System.out.println (

"Exception: " + e.getMessage());
      } 
   } 
} 

insert 调用客户端示例

以下代码是 Axis Client 程序示例,该程序将调用 listServiceRequest 操作,以使用提供的详细信息创建/记录新服务请求故障单。

public class ServiceRequestClient
{    


public void main (

String [] args)
   { 


try
      { 
         // Create the request
         ServiceRequestStub                        srqStub     = 

new ServiceRequestStub();
         ServiceRequestStub.Credentials            credentials = 

new ServiceRequestStub.Credentials();
         ServiceRequestStub.ExtendedSettings       extParams   = 

new ServiceRequestStub.ExtendedSettings();
         ServiceRequestStub.ServiceRequest         srqBean     = 

new ServiceRequestStub.ServiceRequest();

         // Initialize Credentials (User Name & User Password / Authorization Token & Slice Token)
         credentials.setUserName (

"wsuser");
         credentials.setUserPassword (

"wsuser");

         // Initialize Extended Settings such as Response Format (XML, JSON)
         extParams.setResponseFormat (

"JSON");

         // Initialize Service Request Bean with neccessary details
         srqBean.setRequester_name (

"Admin, InteQ");
         srqBean.setTicket_description (

"Ticket creation via webservice# " + 

new Date());

         // Invoke the INSERT service
         ServiceRequestStub.DefaultServiceResponse serviceResponse = srqStub.logServiceRequest (credentials, extParams, srqBean);

         // Inspect successful execution of service and retrieve the response text


if (serviceResponse.getResponseStatus ().equals (

"OK"))
         { 


System.out.println(

"JSON Response : " + serviceResponse.getResponseText ());  
         } 
         // Retrieve the status code, status message and error messages, in 

case of failures


else
         { 


System.out.println(

"Status Code : " + serviceResponse.getStatusCode ());


System.out.println(

"Status Message : " + serviceResponse.getStatusMessage ());


System.out.println(

"Errors : " + Arrays.asList (serviceResponse.getErrors ()).toString ());
         } 
      } 


catch (Exception e) 
      { 


System.out.println (

"Exception: " + e.getMessage());
      } 
   } 
} 

update 调用客户端示例

 

以下代码是 Axis Client 程序示例,该程序将调用 listServiceRequest 操作,以使用提供的详细信息更新由 row_id “123”标识的现有服务请求故障单。

public class ServiceRequestClient
{    


public void main (

String [] args)
   { 


try
      { 
         // Create the request
         ServiceRequestStub                        srqStub     = 

new ServiceRequestStub();
         ServiceRequestStub.Credentials            credentials = 

new ServiceRequestStub.Credentials();
         ServiceRequestStub.ExtendedSettings       extParams   = 

new ServiceRequestStub.ExtendedSettings();
         ServiceRequestStub.ServiceRequest         srqBean     = 

new ServiceRequestStub.ServiceRequest();

         // Initialize Credentials (User Name & User Password / Authorization Token & Slice Token)
         credentials.setUserName (

"wsuser");
         credentials.setUserPassword (

"wsuser");

         // Initialize Extended Settings such as Response Format (XML, JSON)
         extParams.setResponseFormat (

"JSON");

         // Initialize Service Request Bean with neccessary details
         srqBean.setRow_id (123);
         srqBean.setTicket_description (

"Updated - Ticket creation via webservice# " + 

new Date());

         // Invoke the UPDATE service
         ServiceRequestStub.DefaultServiceResponse serviceResponse = srqStub.logServiceRequest (credentials, extParams, srqBean);

         // Inspect successful execution of service and retrieve the response text


if (serviceResponse.getResponseStatus ().equals (

"OK"))
         { 


System.out.println(

"JSON Response : " + serviceResponse.getResponseText ());  
         } 
         // Retrieve the status code, status message and error messages, in 

case of failures


else
         { 


System.out.println(

"Status Code : " + serviceResponse.getStatusCode ());


System.out.println(

"Status Message : " + serviceResponse.getStatusMessage ());


System.out.println(

"Errors : " + Arrays.asList (serviceResponse.getErrors ()).toString ());
         } 
      } 


catch (Exception e) 
      { 


System.out.println (

"Exception: " + e.getMessage());
      } 
   } 
} 

delete 调用客户端示例

以下代码是 Axis Client 程序示例,该程序调用 deleteAttachment 操作,并从 Row Id# 12 标识的数据库中删除附件文件条目。                  

public class AttachmentClient
{    


public void main (

String [] args)
   { 


try
      { 
         // Create the request
         AttachmentStub                  attachmentStub = 

new AttachmentStub();
         AttachmentStub.Credentials      credentials    = 

new AttachmentStub.Credentials();
         AttachmentStub.ExtendedSettings extParams      = 

new AttachmentStub.ExtendedSettings();

         // Initialize Credentials (User Name & User Password / Authorization Token & Slice Token)
         credentials.setUserName (

"wsuser");
         credentials.setUserPassword (

"wsuser");

         // Initialize Extended Settings such as Response Format (XML, JSON)
         extParams.setResponseFormat (

"JSON");

         // Invoke the DELETE service
         AttachmentStub.DefaultServiceResponse serviceResponse = attachmentStub.deleteAttachment (credentials, extParams, 

"12");

         // Inspect successful execution of service and retrieve the response text


if (serviceResponse.getResponseStatus ().equals (

"OK"))
         { 


System.out.println(

"JSON Response : " + serviceResponse.getResponseText ());  
         } 
         // Retrieve the status code, status message and error messages, in 

case of failures


else
         { 


System.out.println(

"Status Code : " + serviceResponse.getStatusCode ());


System.out.println(

"Status Message : " + serviceResponse.getStatusMessage ());


System.out.println(

"Errors : " + Arrays.asList (serviceResponse.getErrors ()).toString ());
         } 
      } 


catch (Exception e) 
      { 


System.out.println (

"Exception: " + e.getMessage());
      } 
   } 
} 

© 2017 ServiceAide 1-650-206-8988 http://www.serviceaide.com info@serviceaide.com