接口调用(call 其他程序的接口)

1.调用接口方法

@RestController
@RequestMapping("/admin/operationApiController")
public class OperationApiController {

    @Autowired
    private OperationApiDAO operationApiDAO;

    String https ="https://localhost:8080/";

    public ModelAndView startService(@Valid @ModelAttribute("x")String x, HttpServletRequest request,
                                     HttpServletResponse response, ModelMap modelMap) throws Exception {
        Gson gson =new Gson();
        JsonParser parser =new JsonParser();
        JsonObject jsonObject =(JsonObject)parser.parse(x);
        RequestDTO requestDTO = gson.fromJson(jsonObject, RequestDTO.class);

        String buttonType ="/admin/CallCrossDomain";
        String resp = operationApiDAO.invokeRest(x,https + buttonType);
        modelMap.addAttribute("successString",resp);
        return new ModelAndView("/login/serviceShow");


    }
 }
public class OperationApiDAOImpl implements OperationApiDAO {

    @Override
    public String invokeRest(String json, String url) throws Exception {
        String resp ="";
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost postRequest =new HttpPost(url);
        postRequest.setHeader("Content-Type","application/json");
        StringEntity stringEntity =new StringEntity(json);
        postRequest.setEntity(stringEntity);
        CloseableHttpResponse response = httpClient.execute(postRequest);
        String responStr = EntityUtils.toString(response.getEntity(), "UTF-8");
        if (response.getStatusLine().getStatusCode() != 200){
            throw new Exception("Error:" + responStr);
        }else {
            resp =responStr;
        }
        return resp;
    }
}
@Data
public class RequestDTO {

    private Map<String,String> header;
    private Map<String,Map<String,String>> body;


    public String toString(){
        StringBuffer json =new StringBuffer();
        json.append("{");
        for (String k:header.keySet()){
            json.append(k + ":" + header.get(k) + "/n");
        }
        for (String k:body.keySet()){
            json.append(k + "{");
            Map<String,String> temp =body.get(k);
            for (String key:temp.keySet()){
                json.append(key + ":" + temp.get(key) +"\n");
            }
            json.append("}");
        }
        json.append("}");
        return json.toString();
    }

}

调用的接口 /admin/CallCrossDomain

 @RequestMapping(value = "/admin/CallCrossDomain",method = RequestMethod.POST)
    @ResponseBody
    public String CallCrossDomain(@RequestBody RequestDTO requestDTO){
        String command = requestDTO.getBody().get("data").get("command");
        Runtime rt = Runtime.getRuntime();
        String line="";
        try {
            Process p = rt.exec(command);
            BufferedReader in =new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuffer stringBuffer =new StringBuffer();
            String str="";
            while ((str =in.readLine()) !=null){
                line =stringBuffer.append(str).toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return line;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当我们在使用Node.js和Express框架进行接口调用时,有时会遇到“Maximum call stack size exceeded”错误。这个错误通常是由于递归调用函数或无限循环调用函数导致的。以下是一些可能导致这个错误的原因和解决方法: 1. 递归调用函数:如果你的代码中有递归调用函数,那么可能会导致这个错误。你可以通过检查你的代码并确保递归调用函数的退出条件正确来解决这个问题。 2. 无限循环调用函数:如果你的代码中有无限循环调用函数,那么也可能会导致这个错误。你可以通过检查你的代码并确保循环调用函数的退出条件正确来解决这个问题。 3. 调用栈溢出:如果你的代码中有大量的函数调用,那么可能会导致调用栈溢出。你可以通过增加Node.js的调用栈大小来解决这个问题。你可以在启动Node.js应用程序时使用--stack-size标志来增加调用栈大小,例如: ```shell node --stack-size=10000 app.js ``` 这将增加调用栈大小到10000。 4. 代码错误:如果你的代码中有其他错误,例如死循环或无限递归,那么也可能会导致这个错误。你可以通过检查你的代码并修复错误来解决这个问题。 5. 内存泄漏:如果你的代码中有内存泄漏,那么也可能会导致这个错误。你可以通过使用Node.js的内存分析工具来检查内存泄漏并修复它们来解决这个问题。 以下是一个可能导致“Maximum call stack size exceeded”错误的示例代码: ```javascript function foo() { foo(); } foo(); ``` 这个代码中的函数foo()会无限递归调用自己,最终导致调用栈溢出并抛出“Maximum call stack size exceeded”错误。要解决这个问题,我们需要添加退出条件,例如: ```javascript function foo(count) { if (count > 1000) { return; } foo(count + 1); } foo(0); ``` 这个代码中的函数foo()会递归调用自己,但是添加了退出条件,当count大于1000时,函数将不再递归调用自己,从而避免了调用栈溢出。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值