rejectEmptyResponse
Description
Replaces a response with no content with an empty rejection.
The rejectEmptyResponse
directive is mostly used together with marshallers that serialize to an empty response under certain circumstances. For example serialization of None
. In many cases this empty serialization should instead be handled as 404 Not Found
which is the effect of using rejectEmptyResponse
.
Example
- Scala
-
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." }
- Java