通过 Play 应用程序将 json 文件上传到 Cassandra 数据库时出错
Error uploading json file to a Cassandra DB through a Play application
我想使用 Play application/api 将 json 文件上传到我的 cassandra 数据库。
我的 html 视图如下所示。
@main("Welcome to Play") {
<h1> Upload a JSON file </h1>
@helper.form(action = routes.Application.upload, 'enctype -> "text/json") {
<input type="file" name="jsonFile">
<p>
<input type="submit" value="Upload">
</p>
}}
我在控制器中定义了我的上传方法:
def upload = Action(parse.json) { request =>
val data = Json.arr(request.body)
sc.parallelize(Seq(data)).saveToCassandra("person", "user", SomeColumns("name", "age"))
Redirect("/index") }
我得到的错误信息是
Bad Request: For request 'POST /upload' [Expecting text/json or application/json body]
也尝试使用 enctype "application/json",但没有区别。
如果我尝试打印内容,我得到 Null。
我的file.json的格式是:
{ "name": "alice", "age": 22}
{ "name": "bob", "age": 23}
基本上我似乎无法从文件中正确提取内容。
请参阅文档的这一部分,关于请求正文解析器:
https://www.playframework.com/documentation/2.5.x/ScalaBodyParsers#Choosing-an-explicit-body-parser
基本上,parse.json
要求您使用 application/json
的 Content-Type
提出请求。来自文档:
The json body parser will validate that the request has a Content-Type of application/json, and send back a 415 Unsupported Media Type response if the request doesn’t meet that expectation. Hence we don’t need to check again in our action code.
还有一个正文解析器没有这个要求。同样,来自文档:
This of course means that clients have to be well behaved, sending the correct Content-Type headers with their requests. If you want to be a little more relaxed, you can instead use tolerantJson, which will ignore the Content-Type and try to parse the body as json regardless:
def save = Action(parse.tolerantJson) { request =>
Ok("Got: " + (request.body \ "name").as[String])
}
所以,你有两个选择:
- 使用
parse.json
并将Content-Type
设置为application/json
- 使用
parse.tolerantJson
除此之外,如果内容如下,您JSON文件是无效的:
{ "name": "alice", "age": 22}
{ "name": "bob", "age": 23}
您可以使用 JSONLint and I also recommend you to read the definition at json.org 检查它。您可能想要这样的数组:
[
{ "name": "alice", "age": 22},
{ "name": "bob", "age": 23}
]
格式更正后,您可以使用parse.json
或parse.tolerantJson
。
我想使用 Play application/api 将 json 文件上传到我的 cassandra 数据库。
我的 html 视图如下所示。
@main("Welcome to Play") {
<h1> Upload a JSON file </h1>
@helper.form(action = routes.Application.upload, 'enctype -> "text/json") {
<input type="file" name="jsonFile">
<p>
<input type="submit" value="Upload">
</p>
}}
我在控制器中定义了我的上传方法:
def upload = Action(parse.json) { request =>
val data = Json.arr(request.body)
sc.parallelize(Seq(data)).saveToCassandra("person", "user", SomeColumns("name", "age"))
Redirect("/index") }
我得到的错误信息是
Bad Request: For request 'POST /upload' [Expecting text/json or application/json body]
也尝试使用 enctype "application/json",但没有区别。 如果我尝试打印内容,我得到 Null。
我的file.json的格式是:
{ "name": "alice", "age": 22}
{ "name": "bob", "age": 23}
基本上我似乎无法从文件中正确提取内容。
请参阅文档的这一部分,关于请求正文解析器:
https://www.playframework.com/documentation/2.5.x/ScalaBodyParsers#Choosing-an-explicit-body-parser
基本上,parse.json
要求您使用 application/json
的 Content-Type
提出请求。来自文档:
The json body parser will validate that the request has a Content-Type of application/json, and send back a 415 Unsupported Media Type response if the request doesn’t meet that expectation. Hence we don’t need to check again in our action code.
还有一个正文解析器没有这个要求。同样,来自文档:
This of course means that clients have to be well behaved, sending the correct Content-Type headers with their requests. If you want to be a little more relaxed, you can instead use tolerantJson, which will ignore the Content-Type and try to parse the body as json regardless:
def save = Action(parse.tolerantJson) { request => Ok("Got: " + (request.body \ "name").as[String]) }
所以,你有两个选择:
- 使用
parse.json
并将Content-Type
设置为application/json
- 使用
parse.tolerantJson
除此之外,如果内容如下,您JSON文件是无效的:
{ "name": "alice", "age": 22}
{ "name": "bob", "age": 23}
您可以使用 JSONLint and I also recommend you to read the definition at json.org 检查它。您可能想要这样的数组:
[
{ "name": "alice", "age": 22},
{ "name": "bob", "age": 23}
]
格式更正后,您可以使用parse.json
或parse.tolerantJson
。