rejectEmptyResponse
Signature
def rejectEmptyResponse: Directive0
Description
Replaces a response with no content with an empty rejection.
The rejectEmptyResponse directive is mostly used with marshalling Option[T] instances. The value None is usually marshalled to an empty but successful result. In many cases None should instead be handled as 404 Not Found which is the effect of using rejectEmptyResponse.
Example
val route = rejectEmptyResponse {
path("even" / IntNumber) { i =>
complete {
// returns Some(evenNumberDescription) or None
Option(i).filter(_ % 2 == 0).map { num =>
s"Number $num is even."
}
}
}
}
// tests:
Get("/even/23") ~> Route.seal(route) ~> check {
status shouldEqual StatusCodes.NotFound
}
Get("/even/28") ~> route ~> check {
responseAs[String] shouldEqual "Number 28 is even."
}
Contents