泛微OA调用三方restful接口

本文展示了如何在泛微OA系统中,利用Java Servlet技术调用第三方RESTful接口的具体代码实现,涵盖了后端接口交互的关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接上代码:

package weaver.interfaces.workflow.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;


public class DenyAction implements Action {

    private Log log = LogFactory.getLog(DenyAction.class);

    @Override
    public String execute(RequestInfo requestInfo) {
        // 获取主表数据
        Property[] properties = requestInfo.getMainTableInfo().getProperty();
        Map<String, Object> mainDatas = new HashMap<>();
        for (Property property : properties) {
            mainDatas.put(property.getName(), property.getValue());
        }
        // 构建请求必须的map
        // String xmbhValue = (String) mainDatas.get("xmbh");
       // Map<String, Object> requestMap = new HashMap<>();
       // requestMap.put("xmbh", xmbhValue);

        String dataJson = JSON.toJSONString(mainDatas);

        RestTemplate restTemplate = new RestTemplate();
        String url = "http://192.168.6.112/abc/api/insert";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity entity = new HttpEntity<Object>(dataJson, headers);

        ResponseEntity<JSONObject> res = restTemplate.exchange(URI.create(url), HttpMethod.POST, entity, JSONObject.class);

        // statusCode错误处理
        String statusCode = res.getStatusCode().toString();
        if (res.getStatusCodeValue() != 200) {
            requestInfo.getRequestManager().setMessageid("-2");
            requestInfo.getRequestManager().setMessagecontent("接口状态:" + statusCode + "系统调用外部接口失败!");
            log.error("statusCode" + statusCode + "content:" + requestInfo.getRequestManager().getMessagecontent());
            return Action.FAILURE_AND_CONTINUE;
        }

        JSONObject resBody = res.getBody();
        if (resBody != null) {
            if (resBody.getInteger("state").equals(207) || resBody.getInteger("state").equals(500)) {
                //http调用成功,系统执行失败,返回错误信息,并进行异常提示
                requestInfo.getRequestManager().setMessageid("-2");
                requestInfo.getRequestManager().setMessagecontent("接口状态:" + resBody.getInteger("state") + "系统调用外部接口成功,外部接口执行失败!");
                return FAILURE_AND_CONTINUE;
            } else {
                //系统调用成功,外部系统执行成功
                return SUCCESS;
            }
        }
        return SUCCESS;
    }
}

### OA调用外部API方法 在OA中实现对外部API的调用涉及多个方面,包括但不限于配置环境、编写脚本以及处理数据交互。具体操作如下: 对于E9平台而言,在进行API接口调用前需先获取合法的身份验证令牌(Token),这通常通过用户名密码或其他认证方式完成[^1]。 当准备就绪后,可以利用HTTP请求库发送GET或POST请求至目标URL来访问所需资源。例如,要取得当前节点菜单设置的数据,则应向`http://xxxxxx/api/workflow/OperationMenuSet/getCurrentNodeMenuSetData`发起请求并附带必要的参数和头部信息。 为了简化跨系统的集成工作,可采用桥接模式的思想将复杂的API逻辑封装起来形成易于使用的抽象层——即所谓的“外部业务对象”。以SAP与OA之间的集成为例,“OA流程申请”的概念被引入作为中介者负责管理两者间的消息传递;尽管这里并没有真正实例化任何类实体,却有效实现了松散耦合的设计理念[^2]。 ```python import requests def call_external_api(url, params=None): headers = { 'Authorization': 'Bearer YOUR_ACCESS_TOKEN', 'Content-Type': 'application/json' } response = requests.get(url, headers=headers, json=params) if response.status_code == 200: return response.json() else: raise Exception(f"Failed to fetch data from {url}. Status code: {response.status_code}") ``` 上述代码片段展示了如何构建一个简单的函数用于执行标准RESTful风格的服务调用,并适当加入了异常捕获机制以便更好地应对可能出现的问题场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Miki_souls

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值