rejectEmptyResponse

rejectEmptyResponse

§Signature

§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

  1. val route = rejectEmptyResponse {
  2. path("even" / IntNumber) { i =>
  3. complete {
  4. // returns Some(evenNumberDescription) or None
  5. Option(i).filter(_ % 2 == 0).map { num =>
  6. s"Number $num is even."
  7. }
  8. }
  9. }
  10. }
  11.  
  12. // tests:
  13. Get("/even/23") ~> Route.seal(route) ~> check {
  14. status shouldEqual StatusCodes.NotFound
  15. }
  16. Get("/even/28") ~> route ~> check {
  17. responseAs[String] shouldEqual "Number 28 is even."
  18. }