カスタムJSONマーシャラー/アンマーシャラーの使用に問題があります。このくらいの作品罰金:Akka-http-json「サポートされていないコンテンツタイプ、サポートされているアプリケーション/ json」
trait EWorksJsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
implicit object IndividualJsonFormat extends RootJsonFormat[Individual] {
def write(individual: Individual) = JsObject(
// blah blah blah
)
def read(value: JsValue): Individual = {
// blah blah blah
}
}
問題は、以下のようにUnsupported Content-Type, supported: application/json
が返されることです。
import akka.http.scaladsl.model.ContentTypes._
import akka.http.scaladsl.model.HttpEntity
import akka.http.scaladsl.testkit.ScalatestRouteTest
import akka.http.scaladsl.unmarshalling._
import eworks.model.immutableModel.SpeciesAll
import eworks.model.mutableModel.{Individual, Individuals, VirtualWorld}
import eworks.model.{Fixtures, LoadableModel, SpeciesDefaultLike}
import org.junit.runner.RunWith
import org.scalatest.Matchers._
import org.scalatest._
import org.scalatest.junit.JUnitRunner
import spray.json._
@RunWith(classOf[JUnitRunner])
class TestRest extends WordSpec with SpeciesDefaultLike with LoadableModel with ScalatestRouteTest with Fixtures with EWorksJsonSupport {
"EWorksJsonSupport" should {
"work for Individuals" in {
val jsObject: JsValue = harry.toJson
val entity = HttpEntity(`application/json`, jsObject.toString)
Post("/addIndividual", entity) ~> new RestHttp()(speciesDefaults).route ~> check {
handled === true
contentType === `application/json`
status.intValue === 200
val individual1 = Unmarshal(response.entity).to[Individual]
// ErrorFuture(akka.http.scaladsl.unmarshalling.Unmarshaller$UnsupportedContentTypeException: Unsupported Content-Type, supported: application/json)
val individual2 = responseAs[Individual]
responseAs[Individual] shouldBe harry
}
}
}
}
私は 'HttpEntityとしてエンティティを宣言する('アプリケーション/ json'、jsObject.toStringは) ''のContent-type'が設定だろうと思いました。もしそうでなければ、私はそれをどのように達成すべきですか? 進行中の現在の作業と一致するようにコードを修正しました。 あなたが考えるべきことJSONはIDEのIntelliJ IDEAによって提供されるtoString出力です。これを理解するために貼り付けました。あまり混乱していないようだ。 「HttpResponse」はどこから取得しましたか? –
また、 'Content-Type'のテストが渡されました。これは混乱します。 –
問題は 'Post("/addIndividual "、エンティティ)'呼び出しに提供するエンティティ 'entity'ではなく、アプリケーションが返すエンティティ(' response.entity')であり、これは型です'text/plain'であり、有効なJSONを含んでいません。それを修正する必要があります。そこにリストされているコードスニペットに問題はありません。最後の編集の前に、コードスニペットから 'HttpResponse'を取得しました。これはレスポンスの内容を示しています。 –