无法调用 findByEmail 因为“this.userRepository”为空错误

时间:2021-03-22 23:30:28

标签: java spring-boot api axios registration

我不断收到无法调用“com.***.repositories.UserRepository.findByEmail(String)”因为“this.userRepository”是空错误每次我尝试处理我的 /register 方法。

我不明白为什么代码会显示为 null。

控制器 -

  col_1  val_1  val_2  new_col
0     A     10     -5        0
1     B     20      0        0
2     C     15      5        0
3     A     25     10      -10
4     B     20      5        0
5     C     15     10        0
6     A     20     15       -5 --> -15 - (-10) --> (-10 is the cumsum of previous rows)
7     B     30     10        0
8     C     40      5        0

axios 调用 -

@Controller
@RestController
@RequestMapping("/api/v1")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class AuthenticationController {

    @Autowired
    UserService userService;
    UserRepository userRepository;
    AuthenticationTokenRepository authenticationTokenRepository;

    @PostMapping(value = "/register")
    public ModelAndView registerUser(ModelAndView modelAndView, User user) {
        User existingUser = userRepository.findByEmail(user.getEmail());
        if (existingUser != null) {
            modelAndView.addObject("message", "This email already exists!");
            modelAndView.setViewName("error");
        } else {
            userRepository.save(user);

            AuthenticationToken authenticationToken = new AuthenticationToken(user);
            authenticationTokenRepository.save(authenticationToken);

            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setTo(user.getEmail());
            mailMessage.setSubject("Complete Registration!");
            mailMessage.setFrom("***@gmail.com");
            mailMessage.setText("To confirm your account, please click here : "
                    + "http://localhost:8080/confirm-account?token=" + authenticationToken.getConfirmationToken());

            emailSenderService.sendEmail(mailMessage);
            modelAndView.addObject("email", user.getEmail());
            modelAndView.setViewName("successfulRegisteration");
        }
        return modelAndView;
    }

}

我的用户存储库 -

submit() {
  let formData = new FormData();
  formData.set("firstName" , this.firstName)
  formData.set("lastName", this.lastName)
  formData.set("email", this.email)
  formData.set("password", this.password)
  formData.set("captchaToken", this.captchaToken)
  formData.set("agreedToTermsOfService", true)
  axios.post("http://localhost:8080/api/v1/register", formData,
      {headers: {'Content-Type': 'application/json'}})
      .then(res =>  {
        if (res.status === 200) {
          this.$router.push('/register-complete');
        } else {
          console.log(res.data.code);
        }
      })
      .catch(function (err) {
        console.log(err);
      })
}

1 个答案:

答案 0 :(得分:3)

只需将@Autowired 添加到您希望在 spring 之前自动装配的相关字段。

更改您的代码

   @Autowired
    UserService userService;
    UserRepository userRepository;
    AuthenticationTokenRepository authenticationTokenRepository;

   @Autowired
    UserService userService;

   @Autowired
    UserRepository userRepository;

   @Autowired
    AuthenticationTokenRepository authenticationTokenRepository;