在下面的代码中 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;
}