Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception

springboot项目出现Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

运行SpringBootapplication的时候根据ID查询功能:浏览器出现500,idea报如上的错误。

和其他人的错误基本一样,只不过是另一个注解错误。

@ResponseBody
    @RequestMapping("/byId/{id}")
    public Student queryStudentById(@PathVariable Integer id){
        Student student = (Student) redisUtils.get(id.toString());
        if(student != null){
            return student;
        }else {
            student = service.getStudentById(id);
            if (student == null){

                System.out.println("输入id不存在");
                return null;
            }else {
                redisUtils.set(student.getId().toString(), student);
                return student;
            }
        }

    }

其他文章一般是@Autowired和@Service等注解错误
我的错误是没写@PathVariable这个形参注解,好像idea会在函数名有提示,但不太明显。

你可能感兴趣的:(Java,servlet,java,intellij-idea,spring,boot)