我对春天有点陌生,我一直遇到空指针异常。我相信@Autowired
在我的 MongoRepository 上不起作用。出于某种原因,当我尝试一些示例时,它正在工作。(运行函数中注释掉的代码有效)
这是我得到的错误:
2016-05-20 02:31:20.877 错误 6272 --- [nio-8080-exec-2] oaccC[.[.[/].[dispatcherServlet]:Servlet.service() 用于 servlet [dispatcherServlet] path [] 抛出异常 [请求处理失败;嵌套异常是 java.lang.NullPointerException] 的根本原因
java.lang.NullPointerException:com.applesauce.service.CustomerService.addCustomer 处为空(CustomerService.java:24)~[classes/:na]
各位大佬可以看一下指导一下吗?另外,如果我在最佳实践方面做错了什么,请告诉我。如果您需要更多信息,请询问!
com.applesauce.controller
@RestController
@RequestMapping("/customer")
public class CustomerController {
private CustomerService customerService = new CustomerService();
@RequestMapping(value = "/addcustomer", method = RequestMethod.GET)
public Customer addCustomer(@RequestParam("firstName") String fName,
@RequestParam("lastName") String lName,
@RequestParam("email") String email,
@RequestParam("phoneNumber") String phoneNumber,
@RequestParam("source") String source){
return customerService.addCustomer(new Customer(fName,lName,email,phoneNumber,source));
}
}
com.applesauce.repository
@Repository
public interface CustomerRepository extends MongoRepository<Customer, String> {
public Customer findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName);
}
com.applesauce.service
@EnableMongoRepositories(basePackages = "com.applesauce.repository")
public class CustomerService {
@Autowired
private CustomerRepository repository;
public Customer addCustomer(Customer customer){
repository.save(customer);
return customer;
}
}