在Mysql表中存储JSON使用akka http

时间:2017-05-10 09:01:28

标签: mysql json akka-http

我正在使用Akka-Http创建rest接口,并使用Slick将POST信息存储在MySql表中。

CREATE TABLE  source (
id INT NOT NULL AUTO_INCREMENT,
type VARCHAR(100),
source_json BLOB,
create_date VARCHAR(10),
modify_date VARCHAR(10),
created_by VARCHAR(100),
modified_by VARCHAR(100),
PRIMARY KEY (id)
);

的Json

{  
"id":"101",
"type":"prototype1",
"description":"pull",
"source_json":[  
  {  
     "v1":"neo",
     "v2":"leo"
  }
],
"create_date":"12/12/12",
"modify_date":"",
"created_by":"User",
"modified_by":"user"
} 

阿卡-HTTP 路由

 val route = (path("CreateSource") & post){
  entity(as [SourceEntity]){sourceEntity :SourceEntity =>
    complete(createDataSource(sourceEntity).map(_.asJson))
  }

案例类

case class SourceEntity(id: Option[Long] = None, `type`: String,  description: String,source_json: String,create_date:String,modify_date:String,created_by:String,modified_by:String){

require(!dataSourceName.isEmpty,“dataSourceName.empty”) }

我需要将source_json值存储到我正在获取的表中

The request content was malformed:

当我进行POST呼叫时

是否归功于Akka Http Unmarshalling?

1 个答案:

答案 0 :(得分:0)

您的SourceEntity案例类指定source_json : String,但实际的json将其定义为字符串数组:

  

" source_json":[
    {
       " V1":"新&#34 ;,        " V2":" LEO"
    }   ]

您只需更改案例类定义:

case class SourceEntity(id: Option[Long] = None, 
                        `type`: String,  
                        description: String,
                        source_json: Array[String],
                        ...)