1 工具类不应该包含 业务的信息
2 如果工具类包含了业务信息,就要做解耦,业务相关的封装成service,通过service调用工具类(不含业务信息的)。
比如以下代码
public class DfCarUtil {
private static final Logger logger = Logger.getLogger(DfCarUtil.class);
private static final Map<String, List<Field>> fieldMap = new ConcurrentHashMap<String, List<Field>>();
public static String postDf(String url, Object requestObj) throws Exception {
if (requestObj == null) {
throw new NullPointerException("requestObj can't be null");
}
// 获取requestObj对象已经父类的所有的public field 需要做缓存
List<Field> allFields = getClassFieldsFromCache(requestObj);
Map<String, Object> params = new TreeMap<String, Object>();
for (Field f : allFields) {
f.setAccessible(true);
if (f.get(requestObj) != null) {
params.put(f.getName(), f.get(requestObj));
}
}
// add app key
// TODO add app key appkey secret 必须可配置 url前缀
params.put("appkey", "kkkkkk");
String signString = DfSignUtil.getSign(params, "xxxxxx");
params.put("sig", signString);
String resp = HttpUtil.post(url, params);
if (logger.isDebugEnabled()) {
logger.debug("resp=" + resp);
}
return resp;
}
private static List<Field> getClassFieldsFromCache(Object obj) {
Class current = obj.getClass();
List<Field> fieldList = fieldMap.get(current.getName());
if (fieldList == null) {
fieldList = createClassFields(current);
fieldMap.put(current.getName(), fieldList);
}
return fieldList;
}
private static synchronized List<Field> createClassFields(Class current) {
List<Field> fieldListCache = fieldMap.get(current.getName());
if (fieldListCache != null)
return fieldListCache;
List<Field> fieldList = new ArrayList<Field>();
putFields(fieldList, current);
while (current.getSuperclass() != null) {
current = current.getSuperclass();
putFields(fieldList, current);
}
return fieldList;
}
private static void putFields(List<Field> list, Class cls) {
Field fds[] = cls.getDeclaredFields();
for (Field f : fds) {
list.add(f);
}
}
public static void main(String args[]) throws Exception {
String urlString = "http://triumph007.vicp.net:89/vip";
ShowCars show = new ShowCars();
show.setLat(1.987);
show.setLng(1.099);
show.setNum(6);
show.setRadius(0.5);
String resp = postDf(urlString + "/customer/showCars", show);
System.out.println(resp);
}
}
设计不是很合理:
url,签名密钥等都封装都utils里面不太合适,,,
要搞个service,,service调用url,密钥等配置,然后service 调用简化后的工具类实现
====================关于异常
public String postdf(PostDfEvent event, Object requestObj) throws Exception {
// 获取requestObj对象已经父类的所有的public field 需要做缓存
List<Field> allFields = getClassFieldsFromCache(requestObj);
Map<String, Object> params = new TreeMap<String, Object>();
if (allFields != null) {
for (Field f : allFields) {
f.setAccessible(true);
if (f.get(requestObj) != null) {
DfProperty dfPropertyAnno = f
.getAnnotation(com.newtaxi.dfcar.web.bean.annotation.DfProperty.class);
if (dfPropertyAnno != null) {
params.put(dfPropertyAnno.value(), f.get(requestObj));
} else {
params.put(f.getName(), f.get(requestObj));
}
}
}
}
// add app key,因为appkey也需要参与排序
params.put("appkey", appkey);
String signString = null;
try {
signString = DfSignUtil.getSign(params, secret);
} catch (Exception e) {
logger.error("getSign error", e);
}
params.put("sig", signString);
String resp = null;
try {
resp = HttpUtil.post(host + event.getPath(), params);
// throw new Exception("test");
} catch (Exception e) {
StringBuilder buf = new StringBuilder();
Set<String> set = params.keySet();
for (Iterator<String> ite = set.iterator(); ite.hasNext();) {
String key = ite.next();
buf.append(key).append("=").append(params.get(key)).append(",");
}
logger.error(
"post to df error, url=" + host + event.getPath() + " post data="
+ buf.toString(), e);
}
if (logger.isDebugEnabled()) {
logger.debug("resp=" + resp);
}
return resp;
}
把可能出错的地方try catch 打日志,,,,这样保证有异常都有日志。 如果让调用方打日志,,,,不能调用方一定会打