spring-boot - 如何在 Spring Controller 中使用 ID(主键)搜索?
问题描述
我正在尝试通过 JpaRepositorygetOne
传递密钥的主键进行搜索
我有一个模型(客户),我在其中定义了客户的详细信息。我有一个接口服务(CustomerService)和它的实现(CustomerServiceImpl,它实现了 CustomerService)。我还有一个接口存储库(CustomerRepository,它扩展了 JpaRepository。现在在我的控制器中,我将一个 id 传递给一个函数(findCustomerById)并使用 JpaRepository 从客户存储库中获取具有该 ID 的客户getOne
客户 customer=customerRepository.getOne(id);
我不断收到错误消息500 (java.lang.NullPointerException: null
。我的代码似乎没问题。可能是什么问题?客户.java
package com.javadevjournal.customer;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="address")
private String address;
public Customer() {
super();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
CustomerRepository.java:
package com.javadevjournal.customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Long>{
}
客户控制器.java:
package com.javadevjournal.customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;
import com.javadevjournal.customer.CustomerService;
import javax.validation.Valid;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@RestController
public class CustomerController {
@Autowired
private CustomerService customerService;
private CustomerRepository customerRepository;
@GetMapping(path = "/customers")
public Iterable<Customer> findAll() {
Iterable<Customer> customers = customerService.findAll();
return customers;
}
// Get a Single Customer
@GetMapping(path = "/getCustomer/{id}")
public Customer findCustomerById(@PathVariable("id") Long id) {
System.out.println("Am going on here...");
System.out.println("id=="+id);
System.out.println("fetching customers.."+id);
Customer customer=customerRepository.getOne(id);
System.out.println(customer);
return customer;
}
// Create a new Customer
@PostMapping("/customers")
public Customer createCustomer(@Valid @RequestBody Customer customer) {
return customerRepository.save(customer);
}
}
我希望根据从邮递员(http://localhost:8080/getCustomer/1)传递的 ID 获得特定客户,但邮递员给了我以下错误消息:
此应用程序没有显式映射 /error,因此您将其视为后备。Tue Jun 25 11:58:47 EAT 2019 出现意外错误(类型=内部服务器错误,状态=500)。在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: 62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod .java:190) 在 org.springframework.web.method.support.InvocableHandlerMethod。
解决方案
你已经忘记@Autowired
添加customerRepository
@Autowired
private CustomerService customerService;
@Autowired
private CustomerRepository customerRepository;
推荐阅读
- excel - 如何使用 VBA 打开此应用程序
- javascript - 如何从 observable 中正确获取数组数据?
- c# - 使用 MSMQ 清除时出现“远程计算机不可用”
- java - 如何在 Jetpack 导航中停止片段重新加载/重新创建
- arrays - 从 SWIFT 中的 API 动态获取字段
- mysql - 如何使用codeigniter从json列中检索数据?
- hash - 在 C 中计算 MD5 - 将输出显示为字符串
- c++ - 通过 C++ 从 PKCS7 (CMS) 获取详细信息
- python - 在 Dash Plotly 中使用实时数据(起点终点延迟)
- unity3d - 纹理不适用于 Unity 中的 .obj 文件