自定义异常的处理和捕获

在下面的代码中 throws new BusinessException(“x10200000000”, “参数为空,不合法”); 为自定义异常,如果遇到异常时在前台打印,就得在controller try catch,而不是throws。serviceImpl 则throws BusinessException 并且在里面自定义异常。

切记:(1)在private 这样的私有方法中是不能写 throw new BusinessException(“x10200000000”, “参数为空,不合法”) 会报错!!!
(2)throws new BusinessException(“x10200000000”, “参数为空,不合法”) 必须是throws 不能是throw,两者性质不一样

 @RequestMapping(value = "/batch", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    @ApiOperation(value = "根据给出的条件查询机构信息", produces = "application/json")
    public Map<String, Object> getOrganizations(@ApiParam(value = "参数类型", defaultValue = "ORGIDS") @RequestParam(value = "search_type", required = false, defaultValue = "ORGIDS") String searchType,
                                                @ApiParam(value = "参数值", required = true) @RequestParam(value = "search_value") String searchValue
    ) {
        Map<String, Object> returnValue = new LinkedHashMap<>();
        String[] orgIds = searchValue.split(",");
        try {
            List<Map<String, Object>> organizations = sysOrgService.getByOrgIds(orgIds);
            this.fillReturnValue(returnValue, organizations);
        } catch (BusinessException e) {
            e.printStackTrace();
            returnValue.put("err_code", e.getCode());
            returnValue.put("err_msg", e.getMessage());
        }
        return returnValue;
    }
@Override
    public List<Map<String, Object>> getByOrgIds(String[] orgIds) throws BusinessException {

        orgIds = this.filterParams(orgIds);
        if(orgIds.length == 0)
            throw new BusinessException("x10200000000", "参数为空,不合法");
        List<Map<String, Object>> result = new ArrayList<>(orgIds.length);
        for(String orgId : orgIds) {
            List<Organization> organizations = organizationRepository.getParentsByOrgId(orgId);
            if(organizations == null || organizations.size() == 0)
                continue;
            int parentIndex = 1;
            int topIndex = organizations.size() - 1;
            Map<String, Object> organizationMap = new LinkedHashMap<>();
            if(organizations.size() == 1)
                parentIndex = 0;
            organizationMap.put("org_id", organizations.get(0).getOrgId());
            organizationMap.put("org_name", organizations.get(0).getOrgName());
            organizationMap.put("order_num", "");
            organizationMap.put("parent_org_id", parentIndex == 0 ? "" : organizations.get(parentIndex).getOrgId());
            organizationMap.put("parent_org_name", parentIndex == 0 ? "" : organizations.get(parentIndex).getOrgName());
            organizationMap.put("top_org_id", organizations.get(topIndex).getOrgId());
            organizationMap.put("top_org_name", organizations.get(topIndex).getOrgName());
            result.add(organizationMap);
        }
        return result;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值