数据格式异常时status码设置为400,服务异常时status码设置为500,并细化服务异常业务状态码

This commit is contained in:
zhangdongxu
2018-08-03 18:08:46 +08:00
parent 4a353a3975
commit 0a10522bae
11 changed files with 498 additions and 263 deletions

View File

@@ -5,13 +5,15 @@ import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Lists;
import com.nis.web.service.SpringContextHolder;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisException;
import com.google.common.collect.Lists;
import com.nis.restful.RestBusinessCode;
import com.nis.restful.ServiceRuntimeException;
import com.nis.web.service.SpringContextHolder;
public class JedisUtils {
private static Logger logger = LoggerFactory.getLogger(JedisUtils.class);
private static JedisPool jedisPool = SpringContextHolder.getBean(JedisPool.class);
@@ -32,7 +34,7 @@ public class JedisUtils {
logger.debug("get {} = {}", key, value);
}
} catch (Exception e) {
throw new RuntimeException("后台错误:" + redisDb + "号redisDB中获取" + key + "对应的值失败", e);
throw new ServiceRuntimeException("" + redisDb + "号redisDB中获取" + key + "对应的值失败", RestBusinessCode.KeyNotExistsInRedis.getValue());
} finally {
returnResource(jedis);
}
@@ -54,7 +56,7 @@ public class JedisUtils {
logger.debug("getObject {} = {}", key, value);
}
} catch (Exception e) {
throw new RuntimeException("后台错误:" + redisDb + "号redisDB中获取" + key + "对应的值失败", e);
throw new ServiceRuntimeException("" + redisDb + "号redisDB中获取" + key + "对应的值失败", RestBusinessCode.KeyNotExistsInRedis.getValue());
} finally {
returnResource(jedis);
}
@@ -95,7 +97,7 @@ public class JedisUtils {
}
logger.debug("set {} = {}", key, value);
} catch (Exception e) {
throw new RuntimeException("后台错误:" + redisDb + "号redisDB中设置zset失败,key=" + key + ",value=" + value, e);
throw new ServiceRuntimeException("" + redisDb + "号redisDB中设置zset失败,key=" + key + ",value=" + value, RestBusinessCode.ZsetFailed.getValue());
} finally {
returnResource(jedis);
}
@@ -117,7 +119,7 @@ public class JedisUtils {
logger.debug("getList {} = {}", key, value);
}
} catch (Exception e) {
throw new RuntimeException("后台错误:" + redisDb + "号redisDB中获取" + key + "对应的值失败", e);
throw new ServiceRuntimeException("" + redisDb + "号redisDB中获取" + key + "对应的值失败", RestBusinessCode.KeyNotExistsInRedis.getValue());
} finally {
returnResource(jedis);
}
@@ -143,7 +145,7 @@ public class JedisUtils {
logger.debug("getObjectList {} = {}", key, value);
}
} catch (Exception e) {
throw new RuntimeException("后台错误:" + redisDb + "号redisDB中获取" + key + "对应的值失败", e);
throw new ServiceRuntimeException("" + redisDb + "号redisDB中获取" + key + "对应的值失败", RestBusinessCode.KeyNotExistsInRedis.getValue());
} finally {
returnResource(jedis);
}
@@ -163,7 +165,7 @@ public class JedisUtils {
result = jedis.exists(key);
logger.debug("exists {}", key);
} catch (Exception e) {
throw new RuntimeException("后台错误:" + redisDb + "号redisDB中判断" + key + "是否存在失败", e);
throw new ServiceRuntimeException("" + redisDb + "号redisDB中判断" + key + "是否存在失败", RestBusinessCode.ExistsKeyFailed.getValue());
} finally {
returnResource(jedis);
}
@@ -183,7 +185,7 @@ public class JedisUtils {
result = jedis.exists(getBytesKey(key));
logger.debug("existsObject {}", key);
} catch (Exception e) {
throw new RuntimeException("后台错误:" + redisDb + "号redisDB中判断" + key + "是否存在失败", e);
throw new ServiceRuntimeException("" + redisDb + "号redisDB中判断" + key + "是否存在失败", RestBusinessCode.ExistsKeyFailed.getValue());
} finally {
returnResource(jedis);
}
@@ -198,14 +200,14 @@ public class JedisUtils {
public static Jedis getResource(int redisDb) throws JedisException {
Jedis jedis = null;
if (jedisPool == null) {
throw new RuntimeException("后台错误:redis连接池为空,请联系管理员检查程序");
throw new ServiceRuntimeException("redis连接池为空,请联系管理员检查程序",RestBusinessCode.CannotConnectionRedis.getValue());
}
try {
jedis = jedisPool.getResource();
jedis.select(redisDb);
} catch (JedisException e) {
returnBrokenResource(jedis);
throw new RuntimeException("后台错误:获取redis连接失败,请联系管理员检查程序", e);
throw new ServiceRuntimeException("获取redis连接失败,请联系管理员检查程序", RestBusinessCode.CannotConnectionRedis.getValue());
}
return jedis;

View File

@@ -9,9 +9,10 @@ import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import com.nis.domain.MaatXmlExpr;
import com.nis.restful.RestBusinessCode;
import com.nis.restful.ServiceRuntimeException;
/**
*
@@ -257,7 +258,7 @@ public class ServiceAndRDBIndexReal {
if (tableName == null || tableName.trim().equals("")) {
if (tableList.size() > 1) {
logger.error("未从业务类型和表对应关系中,找到业务类型:{},配置类型:{}表名:{}对应的真实表名", service, type, tableName);
throw new RuntimeException("业务类型:" + service + ",配置类型:" + type + "对应多个表,请输入具体的表名");
throw new ServiceRuntimeException("在applicationConfig-rule.properties配置文件中业务类型:" + service + ",配置类型:" + type + "对应多个表,请输入具体的表名",RestBusinessCode.NotFoundTableName.getValue());
} else {
return tableList.get(0);
}
@@ -274,8 +275,10 @@ public class ServiceAndRDBIndexReal {
return tableList.get(index);
} else {
logger.error("未从业务类型和表对应关系中,找到业务类型:{},配置类型:{}表名:{}对应的真实表名", service, type, tableName);
throw new RuntimeException(
"后台错误:未从业务类型和表对应关系中,找到业务类型:" + service + ",配置类型:" + type + "表名:" + tableName + "对应的真实表名");
throw new ServiceRuntimeException(
"无法从applicationConfig-rule.properties配置文件中,找到回调类配置service为" + service+ ",配置类型:" + type
+ "对应的真实表名",RestBusinessCode.NotFoundTableName.getValue());
}
}