NutzCN Logo
分享 业务级别RuntimeException处理方案
发布于 2802天前 作者 ecoolper 1886 次浏览 复制 上一个帖子 下一个帖子
标签:

业务级别的RuntimeException类

package com.my;

/**
 * 
 * @author wjw
 *
 */
public class BizRuntimeException extends RuntimeException {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8042926831717014422L;
	
	public BizRuntimeException(String message){
		super(message);
	}
	

}

1 回复

自定义FailProcessor类

package com.my.mvc.processor;

import org.nutz.json.JsonFormat;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionInfo;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.impl.processor.ViewProcessor;

import com.my.BizRuntimeException;


/**
 * 
 * @author wjw
 * 
 */
public class FailProcessor extends ViewProcessor {

	private static final Log log = Logs.get();
	//紧凑格式输出字符串
	private static final JsonFormat format =new JsonFormat();

	@Override
	public void init(NutConfig config, ActionInfo ai) throws Throwable {
		view = evalView(config, ai, ai.getFailView());
	}

	public void process(ActionContext ac) throws Throwable {
		if(ac.getError() instanceof BizRuntimeException){
			BizRuntimeExceptionJson json =new BizRuntimeExceptionJson();
			json.setFlag(false);
			json.setMessage(ac.getError().getMessage());
			Mvcs.write(ac.getResponse(), json, format);
			return;
		} else if(log.isWarnEnabled()) {
			String uri = Mvcs.getRequestPath(ac.getRequest());
			log.warn(String.format("Error@%s :", uri), ac.getError());
		}
		
		super.process(ac);
	}
	
	/**
	 * 返回的json对象
	 * @author wjw
	 *
	 */
	public class BizRuntimeExceptionJson {
		private boolean flag;
		private String message;
		
		public boolean isFlag() {
			return flag;
		}
		public void setFlag(boolean flag) {
			this.flag = flag;
		}
		public String getMessage() {
			return message;
		}
		public void setMessage(String message) {
			this.message = message;
		}
		
	}
	
}

添加回复
请先登陆
回到顶部