rejectEmptyResponse
Replaces a response with no content with an empty rejection.
Signature
def rejectEmptyResponse: Directive0
Description
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."
}
}
}
}
Get("/even/23") ~> sealRoute(route) ~> check {
status === StatusCodes.NotFound
}
Get("/even/28") ~> route ~> check {
responseAs[String] === "Number 28 is even."
}