2016-12-25 4 views
1

私はこのjsonペイロードをspringブートを使用してmysqlに投稿しようとしています。どのようにjsonを含むリストを保持するために、sprinboot javaを使用して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" 
    }] 
} 

私は、モデルクラスを作成しました。エージェントクラス、オーダー、およびQueueInfoを使用して、それらを表すことができます。以下のクラス

@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; 
    } 

} 

は、私は、それぞれのモデルのリポジトリを作成し、それらがCRUD操作を実行することが可能にするためにJPArepositoriesを延びています。

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メソッドを打ったとき、それは持続doesntの。私のデータベーステーブルにはまだ値があります。誰でもここで私を助けることができますか? 私の問題はjsonペイロードの複雑さだと思います。それにリストがあります。どうすればいいですか

+0

エンティティクラスとリポジトリクラスと同じパッケージに 'CsaApiApplication'はありますか? –

答えて

0

問題は、エンティティクラスと比べて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をテストしていませんが、それは動作するはずです....

0

フィールドJSONモデルとJSONモデルの名前は、JavaドメインオブジェクトとJavaオブジェクトモデルと一致しなければならず、自動的にフレームワークによってJavaオブジェクトに変換されます。クラス名には常にSingular Nounを使用することをお勧めします。例えば、OrderUserなど...

関連する問題