NutzCN Logo
问答 ExceptionProcessor如何获取@ok的返回值类型
发布于 1862天前 作者 任杰 1435 次浏览 复制 上一个帖子 下一个帖子
标签:

在下面的拦截异常中,我想获取在此Method的返回值@ok对应的内容,请问如何获取

package com.tohours.pwsleep.processor;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.JsonFormat;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.impl.processor.ViewProcessor;
import org.nutz.mvc.view.JspView;
import org.nutz.mvc.view.UTF8JsonView;
import org.nutz.mvc.view.ViewWrapper;

import com.tohours.pwsleep.exception.BusinessException;
import com.tohours.pwsleep.exception.NotAccessException;
import com.tohours.pwsleep.service.LogService;
import com.tohours.pwsleep.utils.MyResult;
import com.tohours.pwsleep.view.MyJsonView;

@IocBean(singleton = false)
public class ExceptionProcessor extends ViewProcessor {

	private static final Log log = Logs.get();
	
	@Inject
    protected LogService logService;

	@Override
	public void process(ActionContext ac) throws Throwable {
		
		Throwable error = ac.getError();
		NutMap rv;
		//1.0 判定是否是jsonp
		HttpServletRequest req = ac.getRequest();
		String callback = req.getParameter("callback");
		Boolean jsonp = null;
		if(StringUtils.isNotEmpty(callback)) {
			jsonp = true;
		}else {
			jsonp = false;
		}
		
		log.info("jsonp:" + jsonp);
		Boolean is403 = false;
		if (error instanceof NotAccessException) {
			is403 = true;
			log.info("is no access ...");
			rv = new NutMap();
		} else if (error instanceof BusinessException) {
			BusinessException pe = (BusinessException) error;
			rv = new MyResult(pe.getMessage());
			error.printStackTrace();
		} else {
		    String uri = Mvcs.getRequestPath(ac.getRequest());
			if (log.isWarnEnabled()) {
				log.warn(String.format("Error@%s :", uri), ac.getError());
			}
			error.printStackTrace();
			Long id = logService.logError(error, uri);
            rv = new MyResult("系统错误码:" + id);
		}
		String path = ac.getPath();
		Boolean isAdminUri = isAdminUri(path);
		log.info("isAdminUri :" + isAdminUri);
		log.info("exceptionProcesssor rv:" + rv.toString());
		if(!is403) {
			if(jsonp) {
				this.view = new MyJsonView().setJsonp(true).setData(rv);
			}else {
				this.view = new ViewWrapper(new UTF8JsonView(JsonFormat.compact()), rv);
			}
		}else {
			this.view = new JspView("error.403");
		}
		super.process(ac);
	}
	

}

1 回复

你想得到的是@Ok对应的View对象?

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