调整异常内解析json的方法,捕捉异常,并设置字段的默认值

This commit is contained in:
wangxin
2018-06-08 17:48:21 +08:00
parent 15ce519368
commit c9dc82f885
4 changed files with 27 additions and 44 deletions

View File

@@ -8,7 +8,7 @@
*/ */
package com.nis.domain; package com.nis.domain;
import com.google.gson.JsonObject; import org.apache.commons.lang.StringUtils;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
@@ -20,23 +20,29 @@ import net.sf.json.JSONObject;
* @version V1.0 * @version V1.0
*/ */
public class RestfulResult { public class RestfulResult {
private Integer status; private Integer status=0;
private Integer businessCode; private Integer businessCode=0;
private String reason; private String reason="";
private String msg; private String msg="";
private String traceCode; private String traceCode="";
private String fromuri; private String fromuri="";
public RestfulResult(){ public RestfulResult(){
} }
public RestfulResult(String content){ public RestfulResult(String content){
JSONObject resObject = JSONObject.fromObject(content) ; try{
this.status=resObject.getInt("status"); if(StringUtils.isNotBlank(content)){
this.businessCode=resObject.getInt("businessCode"); JSONObject resObject = JSONObject.fromObject(content) ;
this.reason=resObject.getString("reason"); this.status=resObject.getInt("status");
this.msg=resObject.getString("msg"); this.businessCode=resObject.getInt("businessCode");
this.traceCode=resObject.getString("traceCode"); this.reason=resObject.getString("reason");
this.fromuri=resObject.getString("fromuri"); this.msg=resObject.getString("msg");
this.traceCode=resObject.getString("traceCode");
this.fromuri=resObject.getString("fromuri");
}
}catch (Exception e) {
// TODO: handle exception
}
} }
/** /**
* status * status

View File

@@ -2,17 +2,19 @@ package com.nis.exceptions;
import com.nis.domain.RestfulResult; import com.nis.domain.RestfulResult;
public class MaatConvertException extends RuntimeException { public class MaatConvertException extends RuntimeException {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private static final String prefix="<spring:message code=\"request_service_failed\"/>"; private static final String prefix="<spring:message code=\"request_service_failed\"/>:";
private RestfulResult result; private RestfulResult result;
public MaatConvertException(String message) { public MaatConvertException(String message) {
super(message); super(message);
if(message.startsWith(prefix)){ if(message.startsWith(prefix)){
result=new RestfulResult(message.replaceFirst(prefix, "")); String returnValue=message.replaceFirst(prefix, "");
result=new RestfulResult(returnValue);
}else{ }else{
result=new RestfulResult(message); result=new RestfulResult();
} }
} }
@@ -21,7 +23,7 @@ public class MaatConvertException extends RuntimeException {
if(message.startsWith(prefix)){ if(message.startsWith(prefix)){
result=new RestfulResult(message.replaceFirst(prefix, "")); result=new RestfulResult(message.replaceFirst(prefix, ""));
}else{ }else{
result=new RestfulResult(message); result=new RestfulResult();
} }
} }

View File

@@ -150,7 +150,7 @@ public class ConfigServiceUtil {
JSONObject resObject = JSONObject.fromObject(result,config); JSONObject resObject = JSONObject.fromObject(result,config);
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class); bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
}else{ }else{
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>"+response.readEntity(String.class)); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
} }
return bean; return bean;
} }

View File

@@ -84,31 +84,6 @@ public class IpCfgService extends CrudService<IpCfgDao,BaseIpCfg> {
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>"); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
} }
}
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
public void addIpCfg(BaseIpCfg baseIpCfg,List<BaseIpCfg> areaIpCfgs){
// if(areaIpCfgs!=null&&areaIpCfgs.size()>0){
// this.saveIpBatch(areaIpCfgs);
// }
// //调用服务接口获取compileId
// Integer compileId = 0;
// try {
// List<Integer> compileIds = ConfigServiceUtil.getId(1,1);
// if(!StringUtil.isEmpty(compileIds)){
// compileId = compileIds.get(0);
// }
// } catch (Exception e) {
// e.printStackTrace();
// logger.info("获取编译ID出错");
// throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+e.getMessage());
// }
// if(compileId!=0){
// baseIpCfg.setCompileId(compileId);
// return ipCfgDao.insert(baseIpCfg);
// }else{
// throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
// }
} }
/** /**
* *