如何处理 akka http 中的多个无效查询参数?
How to handle multiple invalid query params in akka http?
我在我的路线中定义了查询参数,如下所示:
parameters(('modifiedDate.as[String].?, 'active.as[Boolean].?)) { (modifiedDate, active) =>
我有以下拒绝处理程序:
RejectionHandler.newBuilder()
.handle { case MalformedQueryParamRejection(param, _, _) =>
param match {
case "modifiedDate" => ...
case "active" => ...
}
}
问题是 MalformedQueryParamRejection 只匹配第一个错误,所以如果两个查询参数都被传递并且都不正确:
some-endpoint?modifiedDate=invalid&active=invalid
客户端只会收到第一个错误的通知。
有没有什么方法可以使用 akka http 通知所有无效的查询参数,例如 MalformedQueryParamRejection
,但报告所有无效的查询参数,例如 MalformedQueryParamsRejection
(复数) ?
使用handleAll
。例如:
RejectionHandler.newBuilder()
.handleAll[MalformedQueryParamRejection] { paramRejections =>
// paramRejections is a Seq[MalformedQueryParamRejection]
val malformedParamNames = paramRejections.map(_.parameterName)
...
}
...
我为此提出了功能请求:
我在我的路线中定义了查询参数,如下所示:
parameters(('modifiedDate.as[String].?, 'active.as[Boolean].?)) { (modifiedDate, active) =>
我有以下拒绝处理程序:
RejectionHandler.newBuilder()
.handle { case MalformedQueryParamRejection(param, _, _) =>
param match {
case "modifiedDate" => ...
case "active" => ...
}
}
问题是 MalformedQueryParamRejection 只匹配第一个错误,所以如果两个查询参数都被传递并且都不正确:
some-endpoint?modifiedDate=invalid&active=invalid
客户端只会收到第一个错误的通知。
有没有什么方法可以使用 akka http 通知所有无效的查询参数,例如 MalformedQueryParamRejection
,但报告所有无效的查询参数,例如 MalformedQueryParamsRejection
(复数) ?
使用handleAll
。例如:
RejectionHandler.newBuilder()
.handleAll[MalformedQueryParamRejection] { paramRejections =>
// paramRejections is a Seq[MalformedQueryParamRejection]
val malformedParamNames = paramRejections.map(_.parameterName)
...
}
...
我为此提出了功能请求: