try...catch...finally...

2020-05-13  本文已影响0人  墨色尘埃

异常捕捉,一定要抛出对应的异常类型,否则外层异常可能捕捉不到
比如方法②抛出IOException异常,方法②在方法①中被调用,那么方法①要try捕捉异常,尽量用对应的异常类型捕捉

方法②

    /**
     * 发送请求:GET方式
     */
    public static String sendGetRequestWithMap(String url, String cookie, Map<String, String> map) throws Exception {
        try {
            Connection conn = Jsoup.connect(url);
            conn.method(Connection.Method.GET);
            conn.header("Cookie", cookie);  //请求url带上cookie
            conn.followRedirects(false);
            conn.ignoreContentType(true);
            if (map != null && map.size() > 0) {
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    conn.data(entry.getKey(), entry.getValue());
                }
            }
            Document document = conn.get();
            Connection.Response response = conn.execute();
            String body = response.body();
            while (body.equals("")) {
                body = conn.execute().body();
            }
            return body;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

方法①

    public List<Object> listProjInfos(String ctCookie, String projCode) throws BusinessException {
        logger.info("项目信息查询");
        String passUrl = "https://ctjs.chinaccs.cn/ct/paas/us/commonresource/getProjectPage.do";
        String passCookie = "CurrentOrg_=3202100010" + "; " + "JSESSIONID=" + ctCookie.substring(ctCookie.indexOf("=")
                + 1, ctCookie.length());

        Map map = new HashMap();
        map.put("callback", "angular.callbacks._1k");
        map.put("chargeOrg", "");
        map.put("currentPage", "1");
        map.put("pageSize", "5");
        map.put("pmId", "");
        if (projCode.startsWith("100")) {
            map.put("projCode", projCode);
            map.put("projCustomCode", "");
        } else {
            map.put("projCode", "");
            map.put("projCustomCode", projCode);
        }
        map.put("projName", "");
        map.put("projNature", "");
        map.put("projProcessCode", "");
        map.put("projType", "");

        try {
            String resultJson = RequestUtil.sendGetRequestWithMap(passUrl, passCookie, map);
            //将方法名删除
            resultJson = resultJson.replace("angular.callbacks._1k(", "");
            resultJson = resultJson.substring(0, resultJson.length() - 1);
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            FinancialInvoice financialReview = null;

            financialReview = objectMapper.readValue(resultJson, FinancialInvoice.class);
        } catch (Exception ex) {
            logger.error("项目信息数据格式异常", ex);
            throw new BusinessException("项目信息数据格式异常");
        }

        List<Object> customerDatas = financialReview.getData();
        if (customerDatas == null || customerDatas.size() == 0) {
            throw new BusinessException("项目信息未在祥云中找到");
        }

        return customerDatas;

    }
上一篇下一篇

猜你喜欢

热点阅读