【问题标题】:how to persist a json containing lists, to mysql using sprinboot java如何使用spring boot java将包含列表的json持久化到mysql
【发布时间】:2017-05-10 12:54:27
【问题描述】:

我正在尝试使用 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> {

}

这是我下面的控制器

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 有效负载的复杂性,其中包含列表。我该如何处理呢

【问题讨论】:

  • CsaApiApplication 是否与实体和存储库类在同一个包中?

标签: java mysql json spring list


【解决方案1】:

JSON 和 JSON 模型中的字段名称必须与 java 域对象和 java 对象模型匹配,才能被框架自动转换为 java 对象,并且始终建议使用Singular Nouns 作为类名.例如OrderUser 等...

【讨论】:

    【解决方案2】:

    您面临的问题是由于 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,但它应该可以工作....

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 2021-04-21
      • 2015-04-09
      • 2019-09-18
      • 2022-07-27
      相关资源
      最近更新 更多