json을 반환하는 나머지 서비스에 대한 GET HTTP 호출을 수행합니다. 나는 json을 scala 객체로 파싱하고 싶습니다. 그러나 여기에 저는 붙어 있습니다. 나는 Akka API를 사용하고 있는데이 Akka의 ResponseEntity 여기Scala에서 Akka ResponseEntity의 콘텐츠 가져 오기
에서 내용를 검색 할 관리 할 수있는 것은 내 파일 SBT : 여기
name := "ScalaHttp"
version := "1.0"
scalaVersion := "2.11.8"
libraryDependencies ++={
val akkaV = "2.4.5"
Seq(
"com.typesafe.akka" %% "akka-http-core" % akkaV,
"com.typesafe.play" %% "play-json" % "2.4.0-M3"
)
}
그리고이 응용 프로그램
입니다import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpRequest
import akka.stream.ActorMaterializer
import scala.concurrent.ExecutionContext.Implicits.global
object Sender {
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
Http().singleRequest(HttpRequest(uri = "http://declinators.com/declinator?noun=komunikacja")) foreach {
y => println(y.entity)
println(y.entity.getContentType())
println(y.entity.contentType)
}
}
}
이 인쇄 :
HttpEntity.Strict(application/json,{"nominative":"komunikacja","genitive":"komunikacji","dative":"komunikacji","accusative":"komunikację","instrumental":"komunikacją","locative":"komunikacji","vocative":"komunikacjo"})
application/json
application/json
질문이 올 경우 :
1. ResponseEntity가 getContentType() 및 contentType()을 제공하는 이유는 무엇입니까? 그들은 같은 것을 반환합니다. 2. contentyType을 얻는 것은 쉽습니다. 두 가지 방법이 있지만 콘텐츠 자체를 가져와 json으로 재생할 수 있습니다 (예 : play를 사용하여 구문 분석).
'ByteString'을 반환하는'y.entity.data'를 사용하여 엔티티 데이터에 접근 할 수 있습니다. 귀하의 경우에는'ByteString'에'utf8String'을 호출하여 구문 분석 할 수있는 json 문자열을 얻는 것으로 충분합니다. – thwiegan
getContentType() getter는 Java 호환성을 위해서만 존재합니다. 스칼라에서는'contentType'을 사용해야합니다. – WelcomeTo