我正在尝试Spring-boot使用 Hibernate 和 REST -API 构建 CRUD 应用程序。但是,当我尝试运行该应用程序时,一切正常,但控制台显示以下错误
java.lang.NullPointerException: null
at io.javabrains.EmployerController.getAllEmployers(EmployerController.java:20) ~[classes/:na]
Run Code Online (Sandbox Code Playgroud)
我尝试更改该值,但没有成功
雇主服务.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);
}
}
Run Code Online (Sandbox Code Playgroud)
雇主控制器.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") …Run Code Online (Sandbox Code Playgroud)