首页 新闻 会员 周边 捐助

使用springmvc aop处理异常,拦截器中 HandlerMethod 的getBean()方法获取对象的属性为空

0
悬赏园豆:100 [已关闭问题] 关闭于 2019-04-19 16:03

报错信息

这里输出 page 已经实例化

-------> baseAction Init
BaseAction [logger=Logger[com.nuoyun.pro.action.index.InvestorAction], tip=com.nuoyun.pro.commons.Tip@7fe5d7dd, srh={}, sort_params={}, page=com.tmsps.ne4spring.page.Page@3ed8dde6]
四月 18, 2019 2:47:43 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [SpringMvc] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at com.nuoyun.pro.interceptor.ParamsInterceptor.preHandle(ParamsInterceptor.java:49)
    at org.springframework.web.servlet.HandlerExecutionChain.applyPreHandle(HandlerExecutionChain.java:136)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:986)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

这里是拦截器处理代码

@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        
1.      if (handler.getClass().equals(HandlerMethod.class)) {
2.          // 封装一些公共参数
3.          HandlerMethod handlerMethod = (HandlerMethod) handler;
4.          Object bean = handlerMethod.getBean();
5.          BaseAction baseAction = null;
6.          if (bean instanceof BaseAction) {
7.              baseAction = (BaseAction) bean;
8.          }
9.          if (ChkUtil.isNull(baseAction)) {
10.                 return false;
11.             }
12.             System.out.println(baseAction.toString());
13.         Page page = baseAction.page;
            // 设置分页参数
            if (StringUtils.isNotBlank(request.getParameter("pageNum"))) {
                baseAction.page.setPageNumber(ChkUtil.getInteger(request.getParameter("pageNum")));
            } else {
                baseAction.page.setPageNumber(1);
            }
            if (StringUtils.isNotBlank(request.getParameter("pageSize"))) {
                baseAction.page.setPageSize(ChkUtil.getInteger(request.getParameter("pageSize")));
            } else {
                baseAction.page.setPageSize(12);
            }
        }
        return true;
    }

代码13行这里 获取的 page 为空

InvestorAction继承了BaseAction

public class InvestorAction extends BaseAction {

............

}

BaseAction 构造方法

public BaseAction() {
tip = new Tip();
srh = new JSONObject();
page = new Page();
sort_params = new HashMap<String, String>();
System.out.println("-------> baseAction Init");
}

获取的 page 为啥空??? 请大佬指点迷津

问题补充:
if (handler.getClass().equals(HandlerMethod.class)) {
            // 封装一些公共参数
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Object bean = handlerMethod.getBean();
            BaseAction baseAction = null;
            if (bean instanceof BaseAction) {
                baseAction = (BaseAction) bean;
            }
            Class<? extends BaseAction> class1 = baseAction.getClass();
            Class<?> class2 = Class.forName("com.xxx.pro.base.action.BaseAction");
            Field field = class2.getDeclaredField("page");
            field.setAccessible(true);
            
            if (ChkUtil.isNull(baseAction)) {
                return false;
            }
            
            Page page = new Page();
            JSONObject srh = new JSONObject();
            // 设置分页参数
            if (StringUtils.isNotBlank(request.getParameter("pageNum"))) {
                page.setPageNumber(ChkUtil.getInteger(request.getParameter("pageNum")));
            } else {
                page.setPageNumber(1);
            }
            if (StringUtils.isNotBlank(request.getParameter("pageSize"))) {
                page.setPageSize(ChkUtil.getInteger(request.getParameter("pageSize")));
            } else {
                page.setPageSize(12);
            }
            field.set(baseAction, page);
            Field field2 = class2.getDeclaredField("srh");
            field2.setAccessible(true);
            
        }
        return true;

这个是调试截图

利用反射暂时解决这个问题了,但是还是没弄清楚原因

极客公园的主页 极客公园 | 初学一级 | 园豆:95
提问于:2019-04-18 15:13
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册