我對 Spring Boot 有點陌生,我正在嘗試創建 model/repo/service/serviceImp/controller 型別的架構。
在我嘗試發出這個獲取請求后:
http://localhost:8080/api/v1/people/name?name=steve
我收到了這個錯誤(我在 DB 中創建了幾個人):
"java.lang.NullPointerException: Cannot invoke \"com.project.Springbootbackend.service.PeopleService.findAllByName(String)\" because \"this.peopleService\" is null\r\n\tat com.project.Springbootbackend.controller.PeopleController.findAllByName(PeopleController.java:24)
這是我的代碼:人(物體)
@Entity
public class People {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "name")
private String name;
@Column(name = "surname")
private String surname;
@Column(name = "email")
private String email;
...
//constructor get/set
人員控制器
@RestController
@RequestMapping("/api/v1/people")
@RequiredArgsConstructor
public class PeopleController {
private PeopleService peopleService;
@GetMapping("/name")
public ResponseEntity<List<People>> findAllByName(@RequestParam String name) {
return ResponseEntity.ok().body(peopleService.findAllByName(name));
}
}
人報
public interface PeopleRepository extends JpaRepository<People, Integer> {
List<People> findAllByName(String name);
}
人事服務
public interface PeopleService {
List<People> findAllByName(String name);
}
人事服務小鬼
@RequiredArgsConstructor
@Service
public class PeopleServiceImp implements PeopleService {
PeopleRepository peopleRepository;
@Override
public List findAllByName(String name) {
return (List) ResponseEntity.ok(peopleRepository.findAllByName(name));
}
}
提前謝謝各位。
*解決方案:
物體、服務和存盤庫是相同的。ServiceImp 和控制器更改如下:
控制器:
@RestController
@RequestMapping("/api/v1/people")
public class PeopleController {
private PeopleService peopleService;
public PeopleController(PeopleService peopleService) {
this.peopleService = peopleService;
}
@GetMapping("/name")
public ResponseEntity<List<People>> findAllByName(@RequestParam String name) {
return ResponseEntity.ok().body(peopleService.findAllByName(name));
}
}
服務小鬼
@Service
public class PeopleServiceImp implements PeopleService {
private PeopleRepository peopleRepository;
public PeopleServiceImp(PeopleRepository peopleRepository) {
this.peopleRepository = peopleRepository;
}
@Override
public List<People> findAllByName(String name) {
List<People> people = peopleRepository.findAllByName(name);
return people;
}
}
uj5u.com熱心網友回復:
您的建構式不會注入服務,因為RequiredArgsConstructor(請參閱鏈接)需要特殊處理。因此,使用final:
@RestController
@RequestMapping("/api/v1/people")
@RequiredArgsConstructor
public class PeopleController {
private final PeopleService peopleService;
@GetMapping("/name")
public ResponseEntity<List<People>> findAllByName(@RequestParam String name) {
return ResponseEntity.ok().body(peopleService.findAllByName(name));
}
}
同樣在這里:
@RequiredArgsConstructor
@Service
public class PeopleServiceImp implements PeopleService {
private final PeopleRepository peopleRepository;
@Override
public List findAllByName(String name) {
return (List) ResponseEntity.ok(peopleRepository.findAllByName(name));
}
}
附加提示,使用型別串列:
@Override
public List<People> findAllByName(String name) {
return ResponseEntity.ok(peopleRepository.findAllByName(name));
}
uj5u.com熱心網友回復:
試試這樣:
@Autowired
private PeopleService peopleService;
@Autowired
private PeopleRepository peopleRepository;
您還需要在應用程式的主類中添加@SpringBootApplication注解。
像這樣的東西:
@SpringBootApplication
class PeopleApplication {
public static void main(String[] args) {
...
看看這篇關于 Spring 中自動依賴注入的文章: https ://www.baeldung.com/spring-autowire
uj5u.com熱心網友回復:
您錯過了控制器中的自動裝配注釋以注入可能this.peopleService為空的服務。
@Autowired
private PeopleService peopleService;
您還需要在 serviceimpl 類中進行自動裝配
@Autowired
private PeopleRepository peopleRepository;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/491105.html
上一篇:獲取Javax驗證注解屬性值
