[英]Implementing a Search feature using multiple Search parameters
我是學習 spring 引導的新手,我正在嘗試這個側面項目,您可以在其中使用 2 個參數(access_pin 和 org_name)搜索員工。
假設我想在數據庫中搜索的員工具有以下憑據:
我的 API 應該檢索具有上述匹配項的員工記錄。
我的 REST API URL 看起來像這樣:
http://localhost:8080/employeeSearch/version1/1234/PSG
當我在 PostMan 上發送 GET 請求時,我收到 500 錯誤。
這是數據庫表:
我可以知道 spring 如何處理多個搜索參數並從數據庫返回准確的結果嗎?
我的后端代碼如下所示:
Model: (Employee.java)
package com.example.demo.model;
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 = "employeedatabase")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long employeeId;
@Column(name = "employee_name")
private String employeeName;
@Column(name = "access_pin")
private long accessPin;
@Column(name = "org_name")
private String orgName;
public Employee() {
}
public Employee(String employeeName, String orgName) {
super();
this.employeeName = employeeName;
this.orgName = orgName;
}
public long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(long employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public long getAccessPin() {
return accessPin;
}
public void setAccessPin(long accessPin) {
this.accessPin = accessPin;
}
public String getOrgName() {
return orgName;
}
public void setOrgName(String orgName) {
this.orgName = orgName;
}
}
Controller: (EmployeeController.java)
package com.example.demo.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.Employee;
import com.example.demo.service.EmployeeService;
@RestController
@RequestMapping("/employeeSearch/version1")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
public EmployeeController(EmployeeService employeeService) {
super();
this.employeeService = employeeService;
}
@GetMapping("{accessPin}/{orgName}")
public ResponseEntity<Employee> getEmployeeNAme(@PathVariable("accessPin") long accessPin, @PathVariable("orgName") String orgName) {
Employee result = employeeService.getEmployeeName(accessPin, orgName);
return ResponseEntity.status(HttpStatus.OK).body(result);
}
}
服務: (EmployeeService.java)
package com.example.demo.service;
import com.example.demo.model.Employee;
public interface EmployeeService {
Employee getEmployeeName(long accessPin, String orgName) ;
}
ServiceImpl: (EmployeeServiceImpl.java)
package com.example.demo.serviceImpl;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.exception.ResourceNotFoundException;
import com.example.demo.model.Employee;
import com.example.demo.repository.EmployeeRepository;
import com.example.demo.service.EmployeeService;
@Service
public class EmployeeServiceImpl implements EmployeeService{
@Autowired
private EmployeeRepository employeeRepository;
@Override
public Employee getEmployeeName(long accessPin, String orgName) {
// TODO Auto-generated method stub
// return null;
Optional<Employee> employeeName = employeeRepository.findById(accessPin);
if(employeeName.get().getAccessPin() == accessPin) {
return employeeName.get();
}
else {
throw new ResourceNotFoundException("The employee does not exist. ");
}
}
}
存儲庫: (EmployeeRepository.java)
package com.example.demo.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.demo.model.Employee;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long>{
}
根據您的實體,字段employee_id 是您的ID。 使用 findbyId 時,您正在使用實體的 id 進行搜索。 因此,當您使用“1234”調用 function 時,您將得到一個空的可選項並在調用 Optional::get 時遇到 null 指針異常,從而導致 500 錯誤。 對於您的用例,您可以將自定義 function 添加到 EmployeeRepository,如下所示
Optional<Employee> findByAccessPinAndOrgName(long accessPin, String orgName);
此外,當使用可選值時,請在訪問之前檢查該值是否存在。
請參閱以下文章以獲取有關這兩個主題的更多幫助:
我在您的代碼中看到三個問題。
在您的服務中,您正在調用 findById 但您傳遞的是 accessPin,而不是員工 ID - employeeRepository.findById(accessPin)
。 您應該將findByAccessPin
function 定義添加到您的存儲庫接口。
在您的服務 class 中處理Optional
存儲庫結果時,您應該在嘗試使用.get()
訪問結果之前調用isPresent()
) 。 對空的 Optional 調用 get 將導致拋出異常。
最后,您的EmployeeController
構造函數正在調用 super(),但 class 沒有超級 class。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.