如何使用sprinboot java将包含列表的json持久化到mysql
我试图使用SpringBoot将这个json负载发布到mysql如何使用sprinboot java将包含列表的json持久化到mysql,java,mysql,json,spring,list,Java,Mysql,Json,Spring,List,我试图使用SpringBoot将这个json负载发布到mysql { "id": 74833, "queueName": "NigerianQ", "queueStatus": "expedited", "dateCreated": "12/23/2004", "creator": "Leader", "agents": [{ "name": "James Bond", "agentId": 100 }],
{
"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;
}
}
}
应用程序运行,但当我使用上面的json负载点击post方法时,它不会持久。我的数据库表仍然有空值。有人能帮我吗。
我认为我的问题在于json有效负载的复杂性,它包含列表。如何处理这个问题您面临的问题是由于JSON数据中的字段名与实体类不同。例如,实体中有
agentName
,JSON中有name
以下是一些解决方案
- 将实体类或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,但它应该可以工作。…您面临的问题是,与实体类相比,JSON数据中的字段名不同。例如,实体中有
agentName
,JSON中有name
以下是一些解决方案
- 将实体类或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,但它应该可以工作。…JSON和JSON模型中的字段名必须与java域对象和java对象模型匹配,以便通过框架自动转换为java对象,并且始终建议使用
单数名词
s作为类名。例如,订单
,用户
等 JSON和JSON模型中的字段名必须与java域对象和java对象模型匹配,才能通过框架自动转换为java对象,并且始终建议使用单数名词
s作为类名。例如,订单
,用户
等 CsaApiApplication
是否与实体和存储库类在同一个包中?是否CsaApiApplication
与实体和存储库类在同一个包中?
public interface AgentsRepository extends JpaRepository<Agents, Integer>
{
}
public interface QueueInfoRepository extends JpaRepository<QueueInfo, Integer>
{
}
public interface OrdersRepository extends JpaRepository<Orders, Integer> {
}
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";
}
}
{
"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"
}]
}