我是框架新手,正在尝试集成微服务。我正在使用 WSClient 来调用 Web 服务。为了调用 POST Rest API,我传递了请求正文,其中包含一些必需参数和一些可选参数。我的问题是如何在这里传递可选参数?
我的代码示例是
def add = Action.async { implicit request =>
UserDataForm.bindFromRequest.fold(
profileForm => {
val response = profileForm.errorsAsJson
Future(BadRequest(response).as("application/json"))
},
userData => try {
ws.url("http://localhost:9000/" + "api/profile")
.post(
Map("userId" -> userData.userId, "firstName" -> userData.firstName, "lastName" -> userData.lastName, "country" -> userData.country, "address" -> userData.address)).map { response =>
OutputWithErrors(response.body, response.status)
}
} catch {
case e: Exception =>
Future(BadRequest(Message(false, e.getMessage).toJson.prettyPrint).as("application/json"))
})
}
所以这里address是可选参数。
userData并在此处形成数据
case class UserForm(userId: String, firstName: String, lastName: String, country: String, address: Option[String])
val UserDataForm = Form(
mapping(
"userId" -> nonEmptyText,
"firstName" -> nonEmptyText,
"lastName" -> nonEmptyText,
"country" -> nonEmptyText,
"address" -> optional(text)
)(UserForm.apply)(UserForm.unapply)
)
因此,在ws.url(...)post(Map("userId" -> userData, ...))编译器附近调用休息服务 API 时会抱怨No implicits found for parameter evidence$2: BodyWritable[Map[String, Serializable]]
幕布斯7119047
相关分类