调整异常内解析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;
import com.google.gson.JsonObject;
import org.apache.commons.lang.StringUtils;
import net.sf.json.JSONObject;
@@ -20,23 +20,29 @@ import net.sf.json.JSONObject;
* @version V1.0
*/
public class RestfulResult {
private Integer status;
private Integer businessCode;
private String reason;
private String msg;
private String traceCode;
private String fromuri;
private Integer status=0;
private Integer businessCode=0;
private String reason="";
private String msg="";
private String traceCode="";
private String fromuri="";
public RestfulResult(){
}
public RestfulResult(String content){
JSONObject resObject = JSONObject.fromObject(content) ;
this.status=resObject.getInt("status");
this.businessCode=resObject.getInt("businessCode");
this.reason=resObject.getString("reason");
this.msg=resObject.getString("msg");
this.traceCode=resObject.getString("traceCode");
this.fromuri=resObject.getString("fromuri");
try{
if(StringUtils.isNotBlank(content)){
JSONObject resObject = JSONObject.fromObject(content) ;
this.status=resObject.getInt("status");
this.businessCode=resObject.getInt("businessCode");
this.reason=resObject.getString("reason");
this.msg=resObject.getString("msg");
this.traceCode=resObject.getString("traceCode");
this.fromuri=resObject.getString("fromuri");
}
}catch (Exception e) {
// TODO: handle exception
}
}
/**
* status

View File

@@ -2,17 +2,19 @@ package com.nis.exceptions;
import com.nis.domain.RestfulResult;
public class MaatConvertException extends RuntimeException {
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;
public MaatConvertException(String message) {
super(message);
if(message.startsWith(prefix)){
result=new RestfulResult(message.replaceFirst(prefix, ""));
String returnValue=message.replaceFirst(prefix, "");
result=new RestfulResult(returnValue);
}else{
result=new RestfulResult(message);
result=new RestfulResult();
}
}
@@ -21,7 +23,7 @@ public class MaatConvertException extends RuntimeException {
if(message.startsWith(prefix)){
result=new RestfulResult(message.replaceFirst(prefix, ""));
}else{
result=new RestfulResult(message);
result=new RestfulResult();
}
}

View File

@@ -150,7 +150,7 @@ public class ConfigServiceUtil {
JSONObject resObject = JSONObject.fromObject(result,config);
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
}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;
}

View File

@@ -84,31 +84,6 @@ public class IpCfgService extends CrudService<IpCfgDao,BaseIpCfg> {
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\"/>");
// }
}
/**
*