我正在尝试使用Hibernate和REST -API构建Spring-boot
CRUD应用程序。但是,当我尝试运行该应用程序时,一切运行正常,但控制台显示以下错误
java.lang.NullPointerException: null
at io.javabrains.EmployerController.getAllEmployers(EmployerController.java:20) ~[classes/:na]
我尝试更改该值,但是没有用
EmployerService.java
package io.javabrains;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import io.javabrains.Entity.Employer;
@Service
public class EmployerService {
private Repository repository;
public List<Employer>getAllEmployers(){
List<Employer>employers = new ArrayList<>();
repository.findAll()
.forEach(employers::add);
return employers;
}
public void addEmployer(Employer employer) {
repository.save(employer);
}
}
EmployerController.java
package io.javabrains;
import java.util.List;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import io.javabrains.Entity.Employer;
@RestController
public class EmployerController {
private EmployerService service;
@RequestMapping("/employer")
public List<Employer>getAllEmployers()
{
return service.getAllEmployers();
}
/*
* @RequestMapping("/employer/{id}") public Employer getEmployer(@PathVariable
* int id) { return service.getEmployer(id); }
*/
@RequestMapping(method=RequestMethod.POST,value="/employer/create")
public void addEmployer(@RequestBody Employer employer) {
service.addEmployer(employer);
}
}
....
答案 0 :(得分:0)
在分析代码段时,由于未将EmployerService
指定为对EmployerController
的依赖,因此发生了空指针异常。
Spring不了解依赖关系,因此它不会实例化EmployerService
,因此在EmployerController
中为null。
可以通过自动装配private EmployerService service;
中的EmployerController
将您的EmployerService
更新为以下内容即可
package io.javabrains;
import java.util.List;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import io.javabrains.Entity.Employer;
@RestController
public class EmployerController {
//UPDATE : Autowiring
@Autowired
private EmployerService employerService;
@RequestMapping("/employer")
public List < Employer > getAllEmployers() {
return service.getAllEmployers();
}
/*
* @RequestMapping("/employer/{id}") public Employer getEmployer(@PathVariable
* int id) { return employerService.getEmployer(id); }
*/
@RequestMapping(method = RequestMethod.POST, value = "/employer/create")
public void addEmployer(@RequestBody Employer employer) {
employerService.addEmployer(employer);
}
}
而且,在尝试访问Service
时,repository
中也会发生相同的问题。
更新EmployeeService.java
代码包括@autorwired
逻辑:
package io.javabrains;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import io.javabrains.Entity.Employer;
@Service
public class EmployerService {
@Autowired
private Repository repository;
public List<Employer>getAllEmployers(){
List<Employer>employers = new ArrayList<>();
repository.findAll()
.forEach(employers::add);
return employers;
}
public void addEmployer(Employer employer) {
repository.save(employer);
}
}
答案 1 :(得分:0)
在使用之前,您需要获取存储库的对象,为此在
上添加@Autowiredprivate Repository repository;
在您的EmployerService类中。
答案 2 :(得分:0)
private EmployerService employerService;
这意味着您创建了EmployerService的引用变量,而不是EmployerService的对象。可以通过使用new
关键字来完成。但是正如您所知,Spring Container使用DI(依赖注入)来管理bean(一个对象,在上述情况下为EmployerService的对象)。因此,对象的实例化和整个生命周期都由spring管理。为此,我们必须告诉这个对象应该由spring管理,这是通过使用@Autowired
批注完成的。
答案 3 :(得分:0)
@Autowired 绝对是解决方案。
但是你的服务类,如果你要求你的 REPOSITORY,你也应该把它放在那里。
所以在我的问题中,解决方案是
@Autowired
private ProductService productService;
和
@Autowired
ProductRepository productRepository;