PeopleSoft-PS调用外部接口

REST接口

JAR包-httpclient类实现调用第三方接口

通过对接外部提供的rest接口,将PS系统表中的数据处理完成后,拼接为json字符串推送给对应系统,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
   /+ &recC_EMPL_PRD_REQ as Record +/
/+ Returns String +/

/*
分析JSON数据结构,创建需要的JSON对象或者数组;
根据传入的表获取信息,拼接JSON串;
*/
Local string &reqBody, &rspBody;
/* 创建一个请求json对象*/
Local JsonObject &reqObj = CreateJsonObject();
/*创建一个请求json数组的子对象*/
Local JsonObject &reqObjSub = CreateJsonObject();
/*创建一个请求json数组*/
Local JsonArray &jArr = CreateJsonArray();
/*json对象插入属性*/
&reqObj.AddProperty("message", &reqObj1);
&reqObj1.AddProperty("method", "createRequest");
&reqObj1.AddProperty("workflowid", "750");
&reqObj1.AddProperty("requestname", "转正申请");
&reqObj1.AddProperty("requestlevel", "0");
&reqObj1.AddProperty("creator", &recC_EMPL_PRD_REQ.EMPLID.Value);
&reqObj1.AddProperty("isnextflow", "");
/*json对象插入数组*/
&reqObj1.AddJsonArray("main", &jArr);
/*创建请求json数组的子对象*/
&reqObjSub = CreateJsonObject();
/*将子对象插入json数组中*/
&jArr.AddJsonObjectElement("", &reqObjSub);
/*设置json数组子对象的属性*/
&reqObjSub.AddProperty("fieldname", "bh");
&reqObjSub.AddProperty("fieldvalue", &recC_EMPL_PRD_REQ.C_APP_SEQ.Value);

&reqObjSub = CreateJsonObject();
&jArr.AddJsonObjectElement("", &reqObjSub);
&reqObjSub.AddProperty("fieldname", "sqrq");
&reqObjSub.AddProperty("fieldvalue", &recC_EMPL_PRD_REQ.C_APP_DT.Value);

&reqObjSub = CreateJsonObject();
&jArr.AddJsonObjectElement("", &reqObjSub);
&reqObjSub.AddProperty("fieldname", "sqr1");
&reqObjSub.AddProperty("fieldvalue", &recC_EMPL_PRD_REQ.EMPLID.Value);

&reqObjSub = CreateJsonObject();
&jArr.AddJsonObjectElement("", &reqObjSub);
&reqObjSub.AddProperty("fieldname", "sqrbm1");
&reqObjSub.AddProperty("fieldvalue", &recC_EMPL_PRD_REQ.DEPTID.Value);

&reqObjSub = CreateJsonObject();
&jArr.AddJsonObjectElement("", &reqObjSub);
&reqObjSub.AddProperty("fieldname", "xm1");
&reqObjSub.AddProperty("fieldvalue", &recC_EMPL_PRD_REQ.EMPLID2.Value);

&reqObjSub = CreateJsonObject();
&jArr.AddJsonObjectElement("", &reqObjSub);
&reqObjSub.AddProperty("fieldname", "zzrq");
&reqObjSub.AddProperty("fieldvalue", &recC_EMPL_PRD_REQ.C_PRO_PRD_DT.Value);

&reqObjSub = CreateJsonObject();
&jArr.AddJsonObjectElement("", &reqObjSub);
&reqObjSub.AddProperty("fieldname", "bm1");
&reqObjSub.AddProperty("fieldvalue", &recC_EMPL_PRD_REQ.DEPTID1.Value);

&reqObjSub = CreateJsonObject();
&jArr.AddJsonObjectElement("", &reqObjSub);
&reqObjSub.AddProperty("fieldname", "gw1");
&reqObjSub.AddProperty("fieldvalue", &recC_EMPL_PRD_REQ.POSITION_NBR.Value);

Local string &strC_POSN_GRDE_DESCR;
SQLExec("SELECT G.DESCR from PS_C_POSN_GRDE_VW G WHERE G.OPRID = :1 AND G.SETID = (SELECT P.SETID from ps_C_EMPL_PRD_REQ P WHERE P.C_SECTION_TYPE = '1000000003' AND P.C_APP_SEQ = :2 ) AND G.C_POSN_GRDE_ID = :3 AND G.EFFDT = (SELECT MAX(G1.EFFDT) from PS_C_POSN_GRDE_VW G1 WHERE G1.OPRID = G.OPRID AND G1.SETID = G.SETID AND G1.C_POSN_GRDE_ID = G.C_POSN_GRDE_ID AND G.EFFDT <= SYSDATE )", %OperatorId, &recC_EMPL_PRD_REQ.C_APP_SEQ.Value, &recC_EMPL_PRD_REQ.C_POSN_GRDE_ID.Value, &strC_POSN_GRDE_DESCR);
&reqObjSub = CreateJsonObject();
&jArr.AddJsonObjectElement("", &reqObjSub);
&reqObjSub.AddProperty("fieldname", "ygzj");
&reqObjSub.AddProperty("fieldvalue", &strC_POSN_GRDE_DESCR);

Local string &strLOCATION_DESCR4;
SQLExec("SELECT L.DESCR from PS_LOCATION_TBL L WHERE L.SETID = (SELECT P.SETID from ps_C_EMPL_PRD_REQ P WHERE P.C_SECTION_TYPE = '1000000003' AND P.C_APP_SEQ = :1 ) AND L.LOCATION = :2 AND L.EFF_STATUS = 'A' AND L.EFFDT = (SELECT MAX(L1.EFFDT) from PS_LOCATION_TBL L1 WHERE L1.SETID = L.SETID AND L1.LOCATION = L.LOCATION AND L1.EFFDT <= SYSDATE) ", &recC_EMPL_PRD_REQ.C_APP_SEQ.Value, &recC_EMPL_PRD_REQ.LOCATION.Value, &strLOCATION_DESCR4);
&reqObjSub = CreateJsonObject();
&jArr.AddJsonObjectElement("", &reqObjSub);
&reqObjSub.AddProperty("fieldname", "gzdd");
&reqObjSub.AddProperty("fieldvalue", &strLOCATION_DESCR4);

&reqObjSub = CreateJsonObject();
&jArr.AddJsonObjectElement("", &reqObjSub);
&reqObjSub.AddProperty("fieldname", "zzrq");
&reqObjSub.AddProperty("fieldvalue", &recC_EMPL_PRD_REQ.C_PRD_DT.Value);

&reqObjSub = CreateJsonObject();
&jArr.AddJsonObjectElement("", &reqObjSub);
&reqObjSub.AddProperty("fieldname", "zzyy");
&reqObjSub.AddProperty("fieldvalue", &recC_EMPL_PRD_REQ.ACT_REASON_SUB.Value);

Local string &strACT_REASON_SUB_DESCR;
SQLExec("SELECT RB.descr1 from PS_C_ACT_RES_SUB RB WHERE RB.action_reason = 'PPD' AND RB.act_reason_sub = :1 ", &recC_EMPL_PRD_REQ.ACT_REASON_SUB.Value, &strACT_REASON_SUB_DESCR);
&reqObjSub = CreateJsonObject();
&jArr.AddJsonObjectElement("", &reqObjSub);
&reqObjSub.AddProperty("fieldname", "zzyyms");
&reqObjSub.AddProperty("fieldvalue", &strACT_REASON_SUB_DESCR);


&reqObjSub = CreateJsonObject();
&jArr.AddJsonObjectElement("", &reqObjSub);
&reqObjSub.AddProperty("fieldname", "bcsm");
&reqObjSub.AddProperty("fieldvalue", &recC_EMPL_PRD_REQ.DESCR254.Value);


rem &reqBody = &reqObj.ToString();
/* 接口地址; */
Local string &sURL = "http://oa.akucun.com/WorkflowCustomServiceServlet";
Local string &jsonBody;
/*将json对象转为字符串*/
&jsonBody = &reqObj.ToString();
rem MessageBox(0, "", 0, 0, "&jsonBody==" | &jsonBody);
rem WinMessage("&jsonBody==" | &jsonBody);
/*创建httpclient对象*/
Local JavaObject &jHttp = CreateJavaObject("org.apache.commons.httpclient.HttpClient");
rem &jHttp.getHttpConnectionManager().getParams().setConnectionTimeout(20000);
/*创建postMethod信息对象,包含一些必须的参数,例如:字符集等;*/
Local JavaObject &jMethod = CreateJavaObject("org.apache.commons.httpclient.methods.PostMethod", &sURL);
/*设置postMethod信息对象的setFollowRedirects参数*/
&jMethod.setFollowRedirects( False);
/*设置postMethod信息对象的setRequestHeader参数*/
&jMethod.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

/* get some abritrary JSON from HTML Area in this case because it is easy */
/* JSON in PeopleTools is painful */
Local number &i;
/*
For &i = 1 To &headername.Len
&jMethod.setRequestHeader(&headername [&i], &headevalue [&i]);
End-For;
*/
/*设置postMethod对象的setRequestBody参数,把拼接好的json字符串传入*/
&jMethod.setRequestBody(&jsonBody);
/*httpclient对象调用执行方法,传入配置好的postMethod对象*/
&jHttp.executeMethod(&jMethod);
/******************************* begin 解析回传的JSON ****************************************/
/*解析Json*/
Local JsonParser &JsonParser = CreateJsonParser();
Local boolean &JsonParserStatus = &JsonParser.Parse(&responseBody);
If &JsonParserStatus = False Then
throw CreateException(10003, - 1, "Json解析错误");
End-If;
/*取请求报文参数*/
Local JsonObject &jObj = &JsonParser.GetRootObject();
Local string &strCode = &jObj.GetProperty("code");
rem Local string &strMsg = &jObj.GetProperty("msg");
If &strCode <> "0" Then
C_EMPL_TRA_REQ.C_APPLI_RS.Value = 5;
End-If;
If &responseStatus = 0 Then
MessageBox(0, "", 0, 0, "提交成功!");
Else
MessageBox(0, "", 0, 0, "提交失败!原因:" | &strMsg);
End-If;
/******************************* end 解析回传的JSON ****************************************/
rem 获取返回结果;
Local string &responseBody = &jMethod.getResponseBodyAsString();
Local string &responseStatus = &jMethod.getStatusLine().getStatusCode();
rem MessageBox(0, "", 0, 0, &responseBody | "==" | &responseStatus);
rem &responseBody = CreateJavaObject("java.lang.String", CreateJavaObject("java.lang.String", &responseBody).getBytes("ISO8859-1"), "UTF-8").toString();
&jMethod.releaseConnection();
rem 如果网络状态为200,并且返回的接口编码为:0,表示接口成功接收到推送的数据;否则,表示推送失败,需要把员工转正申请单中的数据删除;
Local string &strReturn = "";
If &responseStatus = "200" And
(Substitute(&responseBody, """code"":""0""", "") <> &responseBody) Then
&strReturn = "";
Else
SQLExec(" DELETE FROM ps_C_EMPL_PRD_REQ p WHERE p.c_app_seq = :1 and p.C_SECTION_TYPE = '1000000003' ", &recC_EMPL_PRD_REQ.C_APP_SEQ.Value);
SQLExec(" COMMIT ");
&strReturn = "接口失败:" | &responseBody;
End-If;
rem MessageBox(0, "", 0, 0, "OA接口的结果&strReturn:" | &strReturn);
Return &strReturn;

PS系统-系统标准类实现调用第三方接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*	
Begin sendRequest
调用外部rest接口;
入参:json对象,url
出参:接口相应参数 或者 报错信息
*/
Function sendRequest(&jsonObj As JsonObject, &url As string) Returns string

Local Message &reqMsg, &rspMsg;
Local string &strRet;
/*集成消息*/
&reqMsg = CreateMessage(Message.IB_GENERIC);
/*连接器ID名称*/
&reqMsg.IBInfo.IBConnectorInfo.ConnectorName = "HTTPTARGET";
/*连接器类型名称*/
&reqMsg.IBInfo.IBConnectorInfo.ConnectorClassName = "HttpTargetConnector";
&bRet = &reqMsg.IBInfo.IBConnectorInfo.AddConnectorProperties("Method", "POST", %HttpProperty);
&bRet = &reqMsg.IBInfo.IBConnectorInfo.AddConnectorProperties("URL", &url, %HttpProperty);
&bRet = &reqMsg.IBInfo.IBConnectorInfo.AddConnectorProperties("Content-Type", "application/json", %HttpHeader);
REM &bRet = &reqMsg.IBInfo.IBConnectorInfo.AddConnectorProperties("TimeOut", "1200000", "HEADER");

&bRet = &reqMsg.SetContentString(&jsonObj.ToString());
&rspMsg = %IntBroker.ConnectorRequest(&reqMsg, True);

If &rspMsg.ResponseStatus = %IB_Status_Success Then
&strRet = &rspMsg.GetContentString();
Else
&strRet = &rspMsg.IBException.ToString()
End-If;
Return &strRet;

End-Function;

/* End sendRequest */



webcervice(SOAP)接口

PS系统-系统标准类实现调用第三方接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

/*拼接soap报文*/
Local string &strSoap = "<?xml version=""1.0""?>";
&strSoap = &strSoap | " <soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:def=""http://DefaultNamespace""> ";
&strSoap = &strSoap | " <soapenv:Header/> ";
&strSoap = &strSoap | " <soapenv:Body> ";
&strSoap = &strSoap | " <def:postRequest> ";
&strSoap = &strSoap | " <def:rootXml><![CDATA[<icip><body>" | &strJsnConfigDtl | "</body></icip>]]></def:rootXml> ";
&strSoap = &strSoap | " </def:postRequest> ";
&strSoap = &strSoap | " </soapenv:Body> ";
&strSoap = &strSoap | " </soapenv:Envelope> ";

/*创建XmlDoc对象*/;
Local XmlDoc &xmlRequest = CreateXmlDoc(&strSoap);
/*创建请求消息*/
Local Message &rqst = CreateMessage(Operation.POSTREQUEST, %IntBroker_Request);
/*将XmlDoc对象插入请求消息中*/
&rqst.SetXmlDoc(&xmlRequest);

try
/*发起请求*/
Local Message &rspn = %IntBroker.SyncRequest(&rqst);
/*获取响应-字符串*/
Local string &ResponseStr = &rspn.GenXMLString();

/*记录响应日志*/
&pcSsoLog = GetFile("/home/psadm2/psft/pt/8.56/file/LOG/ZJ_PAY_" | Month(%Date) | ".log", "A", %FilePath_Absolute);
&pcSsoLog.WriteLine(&ResponseStr);
&pcSsoLog.Close();

/*获取响应-XmlDoc*/
Local XmlDoc &rspnXmlDoc = &rspn.GetXmlDoc();

/*获取响应-return节点*/
Local XmlNode &returnNode = &rspnXmlDoc.GetElementsByTagName("return")[1];
Local XmlDoc &xmlReturnDoc = CreateXmlDoc(&returnNode.NodeValue);

/*获取响应-body节点*/
Local XmlNode &bodyNode = &xmlReturnDoc.GetElementsByTagName("body")[1];
Local string &strBodyNode = &bodyNode.NodeValue;

/*解析body节点,转为Json对象*/
Local JsonParser &jResponseJson = CreateJsonParser();
Local boolean &bParseRslt = &jResponseJson.Parse(&strBodyNode);
Local JsonObject &jRootObj = &jResponseJson.GetRootObject();
Local string &strCode = &jRootObj.GetProperty("code"); /*状态编码,接收状态,非200:失败,200:成功*/
Local string &strMsg = &jRootObj.GetProperty("msg"); /*描述信息,当接收状态为“非200”时,描述接收错误原因*/

If &strCode = "200" Then /*成功*/
/*状态:成功*/
Else
/*状态:失败*/
End-If;
catch exception &ex
/*捕获异常*/
end-try;

常见问题

请求异常

注意,当请求报错Http无法连接时,检查如下: