я добавил обработчик отклонения в моих маршрутах, воспользовался (здесь) [https://doc.akka.io/docs/akka-http/current/routing-dsl/rejections.html]
вот мой код
implicit def myRejectionHandler:RejectionHandler =
RejectionHandler.newBuilder()
.handle {
case MissingCookieRejection(cookieName) =>
val errorResponse = routeResponseMessage.getResponse(StatusCodes.BadRequest.intValue
,ServerMessages.MISSING_COOKIE,JsObject.empty)
complete(BadRequest,errorResponse)
}
.handle {
case AuthorizationFailedRejection =>
val errorResponse = routeResponseMessage.getResponse(StatusCodes.Forbidden.intValue
,ServerMessages.AUTH_FAIL,JsObject.empty)
complete((Forbidden, errorResponse))
}
.handle {
case ValidationRejection(msg, _) =>
val errorResponse = routeResponseMessage.getResponse(StatusCodes.InternalServerError.intValue
,ServerMessages.VALIDATION_FAILED + msg,JsObject.empty)
complete((InternalServerError, errorResponse))
}
.handle {
case MissingQueryParamRejection(param) =>
val errorResponse = routeResponseMessage.getResponse(StatusCodes.BadRequest.intValue
,param + " " + ServerMessages.MISSING_PARAM,JsObject.empty)
complete((BadRequest, errorResponse))
}
.handleNotFound {
val errorResponse = routeResponseMessage.getResponse(NotFound.intValue
,ServerMessages.NOT_FOUND,JsObject.empty)
complete((NotFound, errorResponse))
}.result()
val myExceptionHandler = ExceptionHandler {
case _: Exception =>
extractUri { uri =>
log.error(s"Request to $uri could not be handled normally")
complete(InternalServerError, routeResponseMessage.getResponse(InternalServerError.intValue, ServerMessages.INTERNAL_SERVER_ERROR, JsObject.empty))
}
}
val route: Route = handleRejections(myRejectionHandler) {
handleExceptions(myExceptionHandler) {
concat(
path("event-by-id") {
get {
parameters('id.as[String]) {
id =>
//logic here
}}}
post {
path("create-event") {
entity(as[Event]) {
event =>
//logic here
}}
}
val bindingFuture = Http().bindAndHandle(route, hostName, port)
когда я Хит localhost:8080/blah
Возвращенное сообщение было
HTTP method not allowed, supported methods: POST
, но оно должно отображать мое пользовательское сообщение, которое я дал handleNotFound
Что я здесь не так делаю?