异常response处理
This commit is contained in:
@@ -227,4 +227,27 @@ public class SysDepartmentDTO {
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
|
||||
|
||||
public static RegionDO convertRegionDO(SysDepartmentDTO dept, Multimap<String, String> leaderDeptMap, RegionDO parentRegion){
|
||||
RegionDO region = new RegionDO();
|
||||
region.setRegionId(dept.getId());
|
||||
region.setName(dept.getName());
|
||||
region.setParentId(dept.getParentId());
|
||||
region.setUnclassifiedFlag(CommonConstants.ZERO);
|
||||
region.setLeaderUserId(dept.getLeaderUserId());
|
||||
region.setOrderNum(dept.getDepartOrder());
|
||||
region.setThirdDeptId(dept.getId());
|
||||
region.setCreateTime(System.currentTimeMillis());
|
||||
region.setUpdateTime(System.currentTimeMillis());
|
||||
String regionPath = parentRegion.getRegionPath() + region.getRegionId() + CommonConstants.PATH_SPILT;
|
||||
region.setRegionPath(regionPath);
|
||||
region.setDeleted(Boolean.FALSE);
|
||||
if(CollectionUtils.isNotEmpty(dept.getDeptManagerUseridList())){
|
||||
for (String leader : dept.getDeptManagerUseridList()) {
|
||||
leaderDeptMap.put(leader, dept.getId());
|
||||
}
|
||||
}
|
||||
return region;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,26 @@
|
||||
package com.cool.store.handler;
|
||||
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
import com.cool.store.constants.CommonConstants;
|
||||
import com.cool.store.exception.ApiException;
|
||||
import com.cool.store.exception.ServiceException;
|
||||
import com.cool.store.response.ResponseResult;
|
||||
import com.cool.store.response.error.ErrorResponse;
|
||||
import com.cool.store.utils.UUIDUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author zhangchenbiao
|
||||
* @FileName: CustomExceptionHandler
|
||||
@@ -21,24 +32,41 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
public class CustomExceptionHandler {
|
||||
|
||||
@ExceptionHandler(value = ServiceException.class)
|
||||
public ResponseEntity<ErrorResponse> handleCustomException(ServiceException e) {
|
||||
public void handleCustomException(ServiceException e, HttpServletResponse httpServletResponse) {
|
||||
log.error(e.getMessage(), e);
|
||||
ErrorResponse errorResponse = new ErrorResponse(e.getErrorCode(), e.getMessage());
|
||||
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
|
||||
ResponseResult responseResult = new ResponseResult(e.getErrorCode(), e.getMessage());
|
||||
responseResult(httpServletResponse, responseResult);
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = ApiException.class)
|
||||
public ResponseEntity<ErrorResponse> handleCustomException(ApiException e) {
|
||||
public void handleCustomException(ApiException e, HttpServletResponse httpServletResponse) {
|
||||
log.error(e.getMessage(), e);
|
||||
ErrorResponse errorResponse = new ErrorResponse(e.getErrorCode(), e.getMessage());
|
||||
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
|
||||
ResponseResult responseResult = new ResponseResult(e.getErrorCode(), e.getMessage());
|
||||
responseResult(httpServletResponse, responseResult);
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
public ResponseEntity<ErrorResponse> handleException(Exception e) {
|
||||
public void handleException(Exception e, HttpServletResponse httpServletResponse) {
|
||||
log.error(e.getMessage(), e);
|
||||
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
|
||||
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
ResponseResult responseResult = new ResponseResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
|
||||
responseResult(httpServletResponse, responseResult);
|
||||
}
|
||||
|
||||
|
||||
private void responseResult(HttpServletResponse response, ResponseResult result) {
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.setHeader("Content-type", "application/json;charset=UTF-8");
|
||||
String requestId = MDC.get(CommonConstants.REQUEST_ID);
|
||||
if(StringUtils.isBlank(requestId)){
|
||||
requestId = UUIDUtils.get32UUID();
|
||||
}
|
||||
result.setRequestId(requestId);
|
||||
result.setData(null);
|
||||
try {
|
||||
response.getWriter().write(JSONObject.toJSONString(result, SerializerFeature.WriteNullStringAsEmpty));
|
||||
} catch (IOException ex) {
|
||||
log.error(ex.getMessage(),ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -125,6 +125,28 @@ public class ISVHttpRequest {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取部门详情
|
||||
* @param deptId
|
||||
* @return
|
||||
*/
|
||||
public SysDepartmentDTO getDepartmentDetail(String deptId){
|
||||
String url = isvDomain + "/corp/getDepartmentDetail";
|
||||
HashMap requestMap = new HashMap();
|
||||
requestMap.put("deptId", deptId);
|
||||
ResultDTO responseEntity = null;
|
||||
try {
|
||||
responseEntity = httpRestTemplateService.getForObject(url, ResultDTO.class, requestMap);
|
||||
log.info("url:{}, response:{}", url, JSONObject.toJSONString(responseEntity));
|
||||
if(Objects.nonNull(responseEntity.getData()) && responseEntity.isSuccess()){
|
||||
return JSONObject.parseObject(JSONObject.toJSONString(responseEntity.getData()), SysDepartmentDTO.class);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.info("调用isv出错{}", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public AuthScopeDTO getAuthScope(){
|
||||
String url = isvDomain + "/corp/getAuthScope";
|
||||
HashMap requestMap = new HashMap();
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
@@ -80,6 +81,7 @@ public class AliyunServiceImpl implements AliyunService {
|
||||
RecognizeIdentityCardResponse idCardResponse = client.recognizeIdentityCardAdvance(recognizeIdentityCardAdvanceRequest, runtime);
|
||||
log.info("身份证解析结果:{}", JSONObject.toJSONString(idCardResponse));
|
||||
RecognizeIdentityCardResponseBody.RecognizeIdentityCardResponseBodyDataFrontResult frontResult = Optional.ofNullable(idCardResponse).map(o -> o.getBody()).map(o -> o.data).map(o -> o.frontResult).orElse(null);
|
||||
if(Objects.nonNull(frontResult)){
|
||||
String username = frontResult.name;
|
||||
String liveAddress = frontResult.address;
|
||||
String birthdate = frontResult.birthDate;
|
||||
@@ -92,6 +94,8 @@ public class AliyunServiceImpl implements AliyunService {
|
||||
IdentityCardInfoVO result = new IdentityCardInfoVO(username, liveAddress, birthdate, sex, idCard, nation);
|
||||
log.info("身份证解析:{}", JSONObject.toJSONString(result));
|
||||
return result;
|
||||
}
|
||||
return null;
|
||||
} catch (com.aliyun.tea.TeaException e) {
|
||||
log.error("身份证解析报错TeaException:{}", e);
|
||||
throw new ApiException(e.getMessage());
|
||||
|
||||
@@ -209,9 +209,14 @@ public class EnterpriseSyncServiceImpl implements EnterpriseSyncService {
|
||||
|
||||
@Override
|
||||
public void deptUpdateEvent(DepartmentEventDTO param) {
|
||||
SysDepartmentDTO departmentDetail = isvHttpRequest.getDepartmentDetail(param.getDepartmentId());
|
||||
if(Objects.isNull(departmentDetail)){
|
||||
return;
|
||||
}
|
||||
log.info("部门变更:{}", JSONObject.toJSONString(param));
|
||||
switch (parseValue(param.getEventType())){
|
||||
case DEPARTMENT_CREATED:
|
||||
|
||||
break;
|
||||
case DEPARTMENT_UPDATED:
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user