我正在尝试Spring-boot
使用Hibernate和REST -API 构建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);
}
}
....
在分析给定的代码片段时,发生空指针异常,因为您的代码没有要求spring依赖注入器将其注入EmployerService
为对的依赖EmployerController
,因此它没有将EmployerService
bean类注入到引用中,private EmployerService employerService;
因此in中为null EmployerController
。您可以通过在中添加@Autowire
注释private EmployerService service;
引用来要求Spring Dependency Injector注入依赖项。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);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句