自定义异常类
public class ServiceException extends RuntimeException {
private ErrorCode errorCode;
public ServiceException() {
super();
}
public ServiceException(ErrorCode err) {
super();
this.errorCode = err;
}
public ServiceException(ErrorCode err, String desc) {
this.errorCode = err;
this.errorCode.desc = desc;
}
public ErrorCode getErrorCode() {
return errorCode;
}
}
接口实现类
@Slf4j
public class XxxImpl implements XxxService {
@Resource
private RestTemplate restTempdate;
@override
public void doSomething() {
XxxResp resp;
try {
Map<String, String> map = new HashMap<>();
resp = restTempdate.postForObject(url, map, XxxResp.class);
if (resp.getCode() != 200) {
throw new ServiceException(ErrCode.XX_ERROR);
}
} catch (ServiceException se) {
throw new ServiceException(se.getErrCode());
} catch (Exception e) {
log.error("异常", e);
throw new ServiceException(ErrCode.XX_ERROR);
}
}
}