html - 春季启动 java.lang.NullPointerException: null
问题描述
我正在尝试Spring-boot
使用 Hibernate 和 REST -API 构建 CRUD 应用程序。但是,当我尝试运行该应用程序时,一切正常,但控制台显示以下错误
java.lang.NullPointerException: null
at io.javabrains.EmployerController.getAllEmployers(EmployerController.java:20) ~[classes/:na]
我试图改变这个值,但它没有用
雇主服务.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);
}
}
雇主控制器.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;
因此它在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);
}
}
推荐阅读
- flutter - Flutter - 即使应用程序未打开,是否有办法定期执行方法
- bash - Bash 重定向不适用于 Rscript
- powerbi-desktop - 仅在 powerbi 中创建选定月份的计算去年日期
- perl - 为什么 perl 在已经加载模块时会尝试重新加载模块?
- r - 过滤一系列文件中的特定行元素
- c# - 当行悬停并且列值与另一行匹配时显示工具提示
- express - 带有 expressjs 和 sequelize 的 GraphQl 模式(关联)
- javascript - Electron Windows 应用程序托盘上下文菜单创建持久的不可见电子窗口
- c++ - 将文本文件读入链表 C++
- spring-boot - 你能帮我修复我的 STS 吗?因为它不工作