7

I have defined methods as getJobByID and getJobByName in scala , now I am able to pass the Id parameter during the GET call as

val route = (path("dataSource"/LongNumber) & get){ id =>
  complete(getJobById(id).map(_.asJson))
}

Now I want to get all jobs by name in similar fashion but didn't found any directive which can be used to get the Job name as the parameter and use it to find all job names. Do we have any solution or work around for this?

Vishal
  • 1,442
  • 3
  • 29
  • 48

2 Answers2

11

The Segment path matcher will extract a String value from the path and pass it on as a function argument:

val strRoute : Route = 
  get {
    path("dataSourceByName" / Segment) { jobName : String =>
      ...
    }
  }
Ramón J Romero y Vigil
  • 17,373
  • 7
  • 77
  • 125
1

You can do it this way as well for String.

 def getRoute : Route = get {
        path("dataSourceByName") {
          parameters('name.as[String]) {
            (name) => 
              ...
           }
         }
       }

Similarly for Int instead of as[String] do as[Int]

P.S. : For 'as' to run import following statement: import akka.http.scaladsl.server.Directives._

Ruchir Dixit
  • 105
  • 1
  • 9