gpt4 book ai didi

java - 如何使用 spring boot java 将包含列表的 json 保存到 mysql

转载 作者:行者123 更新时间:2023-11-30 22:07:04 25 4
gpt4 key购买 nike

我正在尝试使用 spring boot 将此 json 有效负载发布到 mysql。

{
"id": 74833,
"queueName": "NigerianQ",
"queueStatus": "expedited",
"dateCreated": "12/23/2004",
"creator": "Leader",
"agents": [{
"name": "James Bond",
"agentId": 100
}],
"OrderData": [{
"id": 23342,
"client_Name": 342442,
"reference_number": 324532452,
"queueName": "Gwarinpa",
"call_after": "12 PM",
"call_before": "11:00AM",
"order_type": "CTV",
"status": "scheduled",
"date_created": "12 / 23 / 2004"
}]
}

我已经创建了模型类:Agents、Orders 和 QueueInfo 以在 mysql 中表示它们。下面是类

@Entity
public class Agents
{
@Id
private int agentId;
private String agentName;

public int getAgentId() {
return agentId;

}
public void setAgentId(int agentId) {
this.agentId = agentId;
}
public String getAgentName() {
return agentName;
}
public void setAgentName(String agentName) {
this.agentName = agentName;
}

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Orders {

@Id
private int id;
private String queueName;
private int reference_number;
private String order_type;
private String call_before;
private String call_after;
private String date_created;
private String client_Name;
private String status;


public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

public String getClient_Name() {
return client_Name;
}
public void setClient_Name(String client_Name) {
this.client_Name = client_Name;
}
public String getQueueName() {
return queueName;
}
public void setQueueName(String queueName) {
this.queueName = queueName;
}
public int getReference_number() {
return reference_number;
}
public void setReference_number(int reference_number) {
this.reference_number = reference_number;
}
public String getOrder_type() {
return order_type;
}
public void setOrder_type(String order_type) {
this.order_type = order_type;
}
public String getCall_before() {
return call_before;
}
public void setCall_before(String call_before) {
this.call_before = call_before;
}
public String getCall_after() {
return call_after;
}
public void setCall_after(String call_after) {
this.call_after = call_after;
}
public String getDate_created() {
return date_created;
}
public void setDate_created(String date_created) {
this.date_created = date_created;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class QueueInfo
{
@Id
private int id;
private String queueName;
private String creator;
private String date_created;
@OneToMany
private List <Agents> agent;
@OneToMany
private List <Orders> orders;
private String queueStatus;




public int getId() {
return id;
}


public void setId(int id) {
this.id = id;
}


public String isQueueStatus() {
return queueStatus;
}


public void setQueueStatus(String queueStatus) {
this.queueStatus = queueStatus;
}


public QueueInfo() {}


public String getQueueName() {
return queueName;
}
public void setQueueName(String queueName) {
this.queueName = queueName;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public String getDate_created() {
return date_created;
}
public void setDate_created(String date_created) {
this.date_created = date_created;
}
public List<Agents> getAgent() {
return agent;
}
public void setAgent(List<Agents> agent) {
this.agent = agent;
}
public List<Orders> getOrders() {
return orders;
}
public void setOrders(List<Orders> orders) {
this.orders = orders;
}

}

我为各自的模型创建了存储库,它们扩展了 JPArepositories 以支持执行 crud 操作。

public interface AgentsRepository extends JpaRepository<Agents, Integer>
{

}

public interface QueueInfoRepository extends JpaRepository<QueueInfo, Integer>
{

}

public interface OrdersRepository extends JpaRepository<Orders, Integer> {

}

下面是我的 Controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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;


@SpringBootApplication
@RestController

@RequestMapping("/queue/create")
public class CsaApiApplication
{
@Autowired
private OrdersRepository Orepo;
@Autowired
private AgentsRepository Arepo;
@Autowired
private QueueInfoRepository Qrepo;



public static void main(String[] args) {
SpringApplication.run(CsaApiApplication.class, args);
}


@RequestMapping(method=RequestMethod.POST)
public String createQueue(@RequestBody QueueInfo queueInfo )
{

Qrepo.save(queueInfo);

return "Created";
}

}

应用程序运行,但是当我使用上面的 json 有效负载点击 post 方法时,它不会持续存在。我的数据库表仍然有空值。有人可以帮我从这里出去吗。我认为我的问题是 json 有效负载的复杂性,其中包含列表。我该如何处理

最佳答案

您面临的问题是由于 JSON 数据中的字段名称与实体类不同。例如,实体中有 agentName,JSON 中有 name

一些解决方案

  • 更改实体类或 JSON 数据以具有匹配的字段名称
  • 在您的实体类中添加额外的注释,以便JSON 到对象映射器 能够连接它们。

例如,匹配这些实体类的正确 JSON 数据是:

{
"id": 74833,
"queueName": "NigerianQ",
"queueStatus": "expedited",
"date_created": "12/23/2004",
"creator": "Leader",
"agent": [{
"agentName": "James Bond",
"agentId": 100
}],
"orders": [{
"id": 23342,
"client_Name": 342442,
"reference_number": 324532452,
"queueName": "Gwarinpa",
"call_after": "12 PM",
"call_before": "11:00AM",
"order_type": "CTV",
"status": "scheduled",
"date_created": "12 / 23 / 2004"
}]
}

我没有用这个 JSON 测试过 POST,但它应该可以工作....

关于java - 如何使用 spring boot java 将包含列表的 json 保存到 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41324796/

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