Akka-Http Json编组和支持问题

时间:2016-06-06 08:10:38

标签: json akka-http spray-json

由于我是Akka-HTTP的新手,我尝试使用Marshalling在Akka-HTTP中实现JSON,并从客户端获取POST数据。但代码产生了错误:

The request content was malformed:
Unexpected end-of-input at input index 0 (line 1, position 1), expected JSON Value:
^

以下是我尝试实施的代码:

主要

import akka.actor.ActorSystem
import akka.event.{Logging, LoggingAdapter}
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import dao.UsersDao
import utils.{Config, MigrationConfig}

import scala.concurrent.ExecutionContext

object Main extends App with Config with MigrationConfig with Routes{
  private implicit val system = ActorSystem()
  protected implicit val executor: ExecutionContext = system.dispatcher
  protected val log: LoggingAdapter = Logging(system, getClass)
  protected implicit val materializer: ActorMaterializer = ActorMaterializer()

  migrate()

  Http().bindAndHandle(handler = logRequestResult("log")(routes), interface = httpInterface, port = httpPort)
}

API:

package api

import dao.UsersDao

import scala.concurrent.ExecutionContext.Implicits.global
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import mappings.JsonMappings
import models.User
import akka.http.scaladsl.server.Directives._
import spray.json._

trait UsersApi extends JsonMappings{
  val usersApi =
    (path("users") & post) { entity(as[User]) { user =>
        complete (UsersDao.create(user).map(_.toJson))
      }
    }
}

我认为错误是从实体发生的(作为[用户])。

package dao

import models.{UserId, User}
import slick.driver.MySQLDriver.api._
import scala.concurrent.Future

object UsersDao extends BaseDao{
  def create(user: User): Future[UserId] = usersTable returning usersTable.map(_.id) += user    
}

JSON映射:

package mappings

import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import models.{Comment, Post, User}
import spray.json.DefaultJsonProtocol

trait JsonMappings extends SprayJsonSupport with DefaultJsonProtocol {
  implicit val userFormat = jsonFormat5(User)
}

请求标题:

POST /v1/users HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 43
Pragma: no-cache
Cache-Control: no-cache
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://localhost
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/48.0.2564.116 Chrome/48.0.2564.116 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://localhost/test.php
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: _ga=GA1.1.1337496085.1458291403

响应标题:

HTTP/1.1 415 Unsupported Media Type
Server: akka-http/2.4.7
Date: Fri, 17 Jun 2016 08:45:08 GMT
Content-Type: text/plain; charset=UTF-8
Content-Length: 71

表格数据:

username=test&password=test&age=21&gender=1

HTML表单:

<form action="http://localhost:8080/v1/users" method="post">
    <input type="text" name="username" value="test">
    <input type="text" name="password" value="test">
    <input type="text" name="age" value="21">
    <input type="text" name="gender" value="1">
    <input type="submit" value="">
</form>

0 个答案:

没有答案