@Autowired,Servlet [dispatcherServlet] 的 Servlet.service() 在路径 [] 的上下文中抛出异常 [......java.lang.NullPointerException]

@Autowired, Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [...... java.lang.NullPointerException]

提问人:meimaymay 提问时间:8/14/2023 最后编辑:meimaymay 更新时间:8/14/2023 访问量:564

问:

即使在将@Autowired添加到“private IUserService userService;”之后,我仍然从运行 LoginController 的代码中收到错误。有人可以帮忙吗?多谢!

“Servlet.service() for servlet [dispatcherServlet] 在路径 [] 的上下文中抛出异常 [请求处理失败;嵌套异常是 java.lang.NullPointerException] 及其根本原因 java.lang.NullPointerException:空”

错误消息在此处输入图像描述

法典

@Controller
@RequestMapping("/login")
@Slf4j 
public class LoginController {


    @Autowired
//    no autowired, nullpointerexception
//    autowired, cannot receive response
    private IUserService userService;


    @RequestMapping("/toLogin") //to log in page 跳转登录页
    public String toLogin(){
        return "login";
    }


    @RequestMapping("/doLogin")
    @ResponseBody
    public RespBean doLogin(LoginVo loginVo) {
        // 接受传参 receive parameter from users with LoginVo
        return userService.doLogin(loginVo);
    }
}


public interface IUserService extends IService<User> {


    RespBean doLogin(LoginVo loginVo);


}

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    private UserMapper userMapper;

    @Override
    public RespBean doLogin(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        // check whether input is empty
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
        // check whether mobile is valid
        if (!ValidatorUtil.isMobile(mobile)) {
            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
        }
        // find user with mobile
        User user = userMapper.selectById(mobile);
        if (user == null) {
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }

        // verify whether user password is correct
        if (!MD5Util.frontPassToDBPass(password, user.getSalt()).equals(user.getPassword())) {
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);

        }
        return RespBean.success();
    }
}

如果没有@Autowired,登录页面会响应用户输入,并显示上面的 NullPointerException。 添加@Autowired后,如果移动输入无效,则没有响应。如果移动和密码输入有效,则登录页面将响应上述 NullPointerException。

我尝试根据下面的网站添加带有@Component的限定符,但它不起作用。

https://www.baeldung.com/spring-autowire

java spring-boot servlet nullpointerexception 自动连线

答:

0赞 Jenia 8/14/2023 #1
  1. 最好通过构造函数实现一个 bean,这样你就可以在 LoginController 类中删除 Autowired 并创建一个带有必要参数的构造函数,你可以使用 Lombok
  2. 由于代码没有编号,因此不清楚 npe 落在哪一行中,但我可以假设在映射器中,您需要检查服务/组件注释是否在映射器类中,并在服务类中使用映射器创建构造函数

评论

0赞 meimaymay 8/15/2023
感谢您的回复!我尝试在类 UserServiceImpl 中添加“@AllArgsConstructor”并将“@Service”、“@Component”添加到映射器类中,但它不起作用。nullpointerexception 指向类 UserServiceImpl 的公共 RespBean doLogin(LoginVo loginVo) 下的这一行“User user = userMapper.selectById(mobile);”,以及类 LoginController 下的这一行“return userService.doLogin(loginVo);”。我的代码还有什么问题吗?@Jenia 谢谢!
0赞 Jenia 8/15/2023
很奇怪,你需要把UserServiceImpl服务RequiredArgsConstructor放在mapper服务上面,它必须工作,如果错误再次发生,很可能不再与mapper = null有关,你可以放一个点来检查它
0赞 meimaymay 8/15/2023
我想通了!在公共类 UserServiceImpl 中的“private UserMapper userMapper;”上方添加“@Autowired”后,它起作用了。这是userMapper注入的问题。非常感谢您的帮助!