在SSM做AOP日志报500错误:Request processing failed; nested exception is java.lang.reflect.UndeclaredThrowableException

报错信息

报错信息

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.reflect.UndeclaredThrowableException

在给controller加功能时,方法参数用了HttpServletRequest和HttpServletResponse, 在LogAop切面类中获取方法的映射出错,就报了500错误。
原代码

package com.oracle.controller;

import com.oracle.pojo.Product;
import com.oracle.service.ProductService;
import com.oracle.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductService productService;

    @RequestMapping("/findProduct")
    @RolesAllowed("ROLE_ADMIN")
    public String findProduct(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                    @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize,
                                    HttpServletRequest request, HttpServletResponse response){
        //ModelAndView mv = new ModelAndView();

        Pager<Product> pager = productService.findAllByCondition(pageNo, pageSize);
        List<Product> list = pager.getList();
        for (Product product : list) {
            System.out.println(product + "==");
        }

        request.getSession().setAttribute("pager",pager);

        return ("/pages/product-list");
    }

}

用ModelAndView来存对象和设置转发的页面即可,就不用HttpServletRequest和HttpServletResponse。
修改后

package com.oracle.controller;

import com.oracle.pojo.Product;
import com.oracle.service.ProductService;
import com.oracle.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductService productService;


    @RequestMapping("/findProduct")
    @RolesAllowed("ROLE_ADMIN")
    public ModelAndView findProduct(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                    @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize){
        ModelAndView mv = new ModelAndView();

        Pager<Product> pager = productService.findAllByCondition(pageNo, pageSize);
        List<Product> list = pager.getList();
        for (Product product : list) {
            System.out.println(product + "==");
        }

        mv.addObject("pager",pager);
        mv.setViewName("/pages/product-list");
        return mv;
    }
}

LogAop切面类

package com.oracle.controller;

import com.oracle.pojo.SysLog;
import com.oracle.service.SysLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import sun.plugin.liveconnect.SecurityContextHelper;

import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Date;

@Component
@Aspect
public class LogAop {

    @Autowired
    HttpServletRequest request;//此步骤要在web.xml配置RequestContextListener
    @Autowired
    SysLogService sysLogService;

    private Date startTime; // 访问时间
    private Class executionClass;// 访问的类
    private Method executionMethod; // 访问的方法
    // 主要获取访问时间、访问的类、访问的方法

    @Before("execution(* com.oracle.controller.*.*(..))")
    public void doBefore(JoinPoint joinPoint) throws NoSuchMethodException {
        System.out.println("执行了before");
        startTime = new Date();//开始访问时间

        //获取要访问的类
        executionClass = joinPoint.getTarget().getClass();

        System.out.println(executionClass.getName());
        //获取访问的方法名
        String methodName = joinPoint.getSignature().getName();

        System.out.println(methodName);
        //获取访问方法的参数
        Object[] args = joinPoint.getArgs();
        if(args == null){//无参数
            System.out.println("执行了空1");
            executionMethod = executionClass.getMethod(methodName);//获取无参方法
            System.out.println("执行了空2");
        }else {
            System.out.println("执行了不空1");
            System.out.println(args.length);
            Class[] classesArgs = new Class[args.length];
            for(int i = 0; i < args.length; i++){
                classesArgs[i] = args[i].getClass();//参数映射
            }
            System.out.println(classesArgs);
            System.out.println(methodName);

            executionMethod = executionClass.getMethod(methodName, classesArgs);//获取有参方法
            System.out.println("执行了不空2");
        }
    }

    @After("execution(* com.oracle.controller.*.*(..))")
    public void doAfter(JoinPoint joinPoint){

        //获取url
        if(executionClass != LogAop.class && executionClass != null && executionMethod != null){//忽略自身
            //第一步:获取类上@RequestMapping对象,即controller类的URLpattern
            RequestMapping classAnnotation = (RequestMapping) executionClass.getAnnotation(RequestMapping.class);
            String[] classRequestMapping = classAnnotation.value();

            //第二步:获取方法上的@RequestMapping
            RequestMapping methodAnnotation = executionMethod.getAnnotation(RequestMapping.class);
            String url = "";
            if(methodAnnotation != null){
                String[] methodRequestMapping = methodAnnotation.value();
                url = classRequestMapping[0] + methodRequestMapping[0];
            }

            //获取ip
            String ip = request.getRemoteAddr();

            //获取当前操作用户,也可以用session获取
            SecurityContext context = SecurityContextHolder.getContext();//从上下文获取当前登录的用户

            User user = (User)context.getAuthentication().getPrincipal();

            String username = user.getUsername();

            SysLog sysLog = new SysLog();
            sysLog.setExecutionTime(new Date().getTime() - startTime.getTime());
            sysLog.setIp(ip);
            sysLog.setUrl(url);
            sysLog.setMethod("[类名]:" + executionClass.getName() + "[方法名]:" + executionMethod.getName());
            sysLog.setUsername(username);
            sysLog.setVisitTime(startTime);
            boolean b = sysLogService.addSysLog(sysLog);
            System.out.println("结果是:" + b);
        }

    }

}

问题解决

Logo

鸿蒙生态一站式服务平台。

更多推荐