gpt4 book ai didi

java - 将 JSON 数组发布到 Spring Boot 2 JPA 中

转载 作者:行者123 更新时间:2023-12-02 11:01:04 25 4
gpt4 key购买 nike

我想这样发帖:

[{
"employeeid": "1111",
"employeename": "YOA"

},
{
"employeeid": "2222",
"employeename": "OYA"
}]

我的 Controller 是这样的:

@PostMapping("/api/employee/save")
public Employee createEmployee(@Valid @RequestBody List<Employee> employee) {
return employeeService.save(employee);
}

型号:

@Entity
@Table(name="EMPLOYEE")
public class Employee implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
int id;
@Column(name = "EMP_ID")
int employeeid;
@Column(name = "EMPLOYEE_NAME")
String employeename;

//GETTER AND SETTER
}

当我发布数据时,我得到的错误如下:

{
"timestamp": "2018-07-13T03:36:25.898+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Invalid property 'id' of bean class [java.util.ArrayList]: Could not find field for property during fallback access!",
"path": "/api/employee/save"
}

服务:

public interface employeeService{

Employee save(List<Employee> employee);

}

服务 import/export :

@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {

@Autowired
EmployeeRepository employeeRepository;

@Override
public Employee save(List<Employee> employee) {
return employeeRepository.save(employee);
}

}

存储库:

public interface EmployeeRepository extends Repository <Employee, Long>
Employee save(List<Employee> employee);
}

错误描述是这样的:bean 类 [java.util.ArrayList] 的属性“id”无效:在回退访问期间找不到属性字段!

使用 saveAll() 时,我收到错误消息:未找到 Employee 类型的属性 saveAll

有谁可以帮助我吗?我修改了我的问题。

问候,我

最佳答案

问题出在这一行

public Employee createEmployee(@Valid @RequestBody List<Employee> employee) {
return employeeService.save(employee); // Problem
}

employeeService.save 在您的案例中只能获取一个实体对象Employee

有两种方法

1.

public Boolean createEmployee(@Valid @RequestBody List<Employee> lstEmployee) {
try{
for(Employee emp : lstEmployee){
employeeService.save(employee);
}
return true;
}catch(Exception e){}
return false;
}

2.

使用saveAll代替

public Employee createEmployee(@Valid @RequestBody List<Employee> employee) {
return employeeService.saveAll(employee);
}

编辑 1:

添加服务类后,看起来您正在手动实现

选项 1:

我建议您直接在 Controller 类中使用EmployeeRepository。通过手动覆盖这些方法,您实际上并不是享受使用存储库的好处

@Autowired
EmployeeRepository employeeRepository;

@PostMapping("/api/employee/save")
public Employee createEmployee(@Valid @RequestBody List<Employee> lstEmployee) {
return employeeRepository.saveAll(lstEmployee);
}

选项 2:

更进一步,像这样改变你的实现。对象可能存在一些错误,但它应该给您一个想法

public interface employeeService{

Employee save(Employee employee);

public <S extends User> List<S> saveAll(Iterable<S> entites);



}

@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {

@Autowired
EmployeeRepository employeeRepository;

@Override
public Employee save(Employee employee) {
return employeeRepository.save(employee);
}

@Override
public List<Employee> saveAll(List<Employee> employee) {
return employeeRepository.saveAll(employee);
}

}

关于java - 将 JSON 数组发布到 Spring Boot 2 JPA 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51317432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com