akka http微服务如何检查请求体参数类型验证?
How to check the request body parameters type validation in akka http micro-services?
我的对象是
case class Request(id:Int,name:String,phone:String)
我在邮递员中的请求是
{
"id": "1205", **here i have changed the request body parameter type Int to String**
"name": "sekhar",
"phone":"1234567890"
}
当我的请求正文字段是错误的数据类型时,如何检查请求参数是否有效
我用过
implicit def myRejectionHandler = RejectionHandler.newBuilder()
.handle {
case MissingQueryParamRejection(param) =>
println(" Test1 ")
val errorResponse = ErrorResponse(BadRequest.intValue, "Missing Parameter", s"The required $param was not found.")
var json:JsValue=Json.toJson(errorResponse)
complete(HttpResponse(BadRequest, entity = HttpEntity(ContentTypes.`application/json`, json.toString())))
}
.handle { case MissingFormFieldRejection(msg) =>
println(" Test2 ")
complete(BadRequest, msg)
}
.handle { case MalformedQueryParamRejection(msg,error,cause) =>
println(" Test3 ")
complete(BadRequest, msg)
}
.handleAll[MethodRejection] { methodRejections =>
val names = methodRejections.map(_.supported.name)
println(" Test4 ")
complete((MethodNotAllowed, s"Can't do that! Supported: ${names mkString " or "}!"))
}
.handleNotFound { complete((NotFound, "Not here!")) }
.result()
val routes: Route = handleRejections(myRejectionHandler) {
//Routes
}
Http().bindAndHandle(routes, "localhost", 8090)
它一次又一次地只接受 handleAll[MethodRejection] 在那个时候改变查询参数(对于 false 参数也是如此)。
如果您使用的是 Spray Json,那么您可能已经为您的案例创建了一种格式 class,它应该如下所示:
假设:
case class Request(id:Int, name:String, phone:String)
你应该有这样的特质:
import spray.json._
trait RequestJsonSupport extends DefaultJsonProtocol {
implicit val requestFormat = jsonFormat3(Request.apply)
}
然后在你的路线上延伸它class:
class MyRouteClass(...) extends RequestJsonSupport {...}
这样你的 Akka Http 实例就知道如何解析 Json 输入并将其转换为你的案例 class。然后你可以担心缺少字段等。喷雾会解决这个问题。
例如,如果您发送了这个:
{
"id": "1205",
"name": "sekhar",
"phone":"1234567890"
}
喷雾会抛出:
The request content was malformed:
Expected Int as JsNumber, but got "1205"
查看 Spray Json 回购 here。
我的对象是
case class Request(id:Int,name:String,phone:String)
我在邮递员中的请求是
{
"id": "1205", **here i have changed the request body parameter type Int to String**
"name": "sekhar",
"phone":"1234567890"
}
当我的请求正文字段是错误的数据类型时,如何检查请求参数是否有效
我用过
implicit def myRejectionHandler = RejectionHandler.newBuilder()
.handle {
case MissingQueryParamRejection(param) =>
println(" Test1 ")
val errorResponse = ErrorResponse(BadRequest.intValue, "Missing Parameter", s"The required $param was not found.")
var json:JsValue=Json.toJson(errorResponse)
complete(HttpResponse(BadRequest, entity = HttpEntity(ContentTypes.`application/json`, json.toString())))
}
.handle { case MissingFormFieldRejection(msg) =>
println(" Test2 ")
complete(BadRequest, msg)
}
.handle { case MalformedQueryParamRejection(msg,error,cause) =>
println(" Test3 ")
complete(BadRequest, msg)
}
.handleAll[MethodRejection] { methodRejections =>
val names = methodRejections.map(_.supported.name)
println(" Test4 ")
complete((MethodNotAllowed, s"Can't do that! Supported: ${names mkString " or "}!"))
}
.handleNotFound { complete((NotFound, "Not here!")) }
.result()
val routes: Route = handleRejections(myRejectionHandler) {
//Routes
}
Http().bindAndHandle(routes, "localhost", 8090)
它一次又一次地只接受 handleAll[MethodRejection] 在那个时候改变查询参数(对于 false 参数也是如此)。
如果您使用的是 Spray Json,那么您可能已经为您的案例创建了一种格式 class,它应该如下所示:
假设:
case class Request(id:Int, name:String, phone:String)
你应该有这样的特质:
import spray.json._
trait RequestJsonSupport extends DefaultJsonProtocol {
implicit val requestFormat = jsonFormat3(Request.apply)
}
然后在你的路线上延伸它class:
class MyRouteClass(...) extends RequestJsonSupport {...}
这样你的 Akka Http 实例就知道如何解析 Json 输入并将其转换为你的案例 class。然后你可以担心缺少字段等。喷雾会解决这个问题。
例如,如果您发送了这个:
{
"id": "1205",
"name": "sekhar",
"phone":"1234567890"
}
喷雾会抛出:
The request content was malformed:
Expected Int as JsNumber, but got "1205"
查看 Spray Json 回购 here。