简体   繁体   中英

What is an entity in Akka-Http?

I am new to akka-http and building a basic server-client application in scala. The examples I looked at has the object "entity". Can someone please explain the concept underlying and why is it used and how is it useful?

post {
    path("insert") {
      entity(as[Student]) {
        obj => complete {
          insertingstudent(obj)
          s"got obj with name ${obj.getName()}"
        }
      }

Thanks

Can someone please explain the concept underlying and why is it used and how is it useful?

entity is of type HttpEntity . From the comments of the code :

Models the entity (aka "body" or "content") of an HTTP message.

It is an abstraction over the content of the HTTP request. Many times, when one sends an HTTP request, they provide a payload inside the body of the request. This body can be in many format, popular ones are JSON and XML.

When you write:

entity(as[Student])

You are attempting to unmarhsall, or deserialize, the body of the request into a data structure of your liking. That means that your obj field in the proceeding function will be of type Student .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM