我是 Play Framework 的新手。我能够通过请求直接发送字符串、整数等简单数据类型,并在后端 Java 方法中访问它们。
当我尝试在路由文件中这样做时,
GET /food/fetchMealInfo/:noOfDays/:dateSelected controllers.trackandplan.FoodController.fetchMealInfo(noOfDays : Integer, dateSelected : Date)
我收到一个错误提示
Compilation error
not found: type Date
将日期对象从前端 AngularJS 应用程序传输到 Play Framework 中的 Java 应用程序的正确、安全和干净的方法是什么。 请指导。
最佳答案
您有几个选择。稍微容易理解的方法是简单地将日期/时间作为 Long
(unix 时间戳)传输,并在 Controller 方法中将其转换为 Date
。
GET /food/fetchMealInfo/:noOfDays/:dateSelected controllers.trackandplan.FoodController.fetchMealInfo(noOfDays: Integer, dateSelected: Long)
public static Result fetchMealInfo(Integer noOfDays, Long dateSelected) {
Date date = new Date(dateSelected.longValue());
...
}
更复杂的方法是使用 PathBindable
,这将允许您在路由文件本身中使用 Date
。但是,您仍然需要将 Date
作为 Long
传输(如果可能,PathBindable
将进行转换)。不幸的是,由于我们显然无法控制 Date
,我们必须在 Scala 中实现 PathBindable
,而不是 Java(Java 需要为 Date 实现一个接口(interface)
,我们做不到)。
app/libs/PathBinders.scala
package com.example.libs
import java.util.Date
import play.api.mvc.PathBindable
import scala.util.Either
object PathBinders {
implicit def bindableDate(implicit longBinder: PathBindable[Long]) = new PathBindable[Date] {
override def bind(key: String, value: String): Either[String, Date] = {
longBinder.bind(key, value).right.map(new Date(_))
}
override def unbind(key: String, date: Date): String = key + "=" + date.getTime().toString
}
}
为了让路由文件能够选择它,您需要将以下内容添加到您的 build.sbt
文件中:
PlayKeys.routesImport += "com.example.libs.PathBinders._"
PlayKeys.routesImport += "java.util.Date"
现在您可以在路由文件中使用Date
(作为Long
),而无需为使用它的每个方法专门处理它。
GET /food/fetchMealInfo/:noOfDays/:dateSelected controllers.trackandplan.FoodController.fetchMealInfo(noOfDays: Integer, dateSelected: Date)
注意:如果您使用的是较旧的 Play 版本,这可能不会立即编译。我用 Play 2.3.8 和 sbt 0.13.5 测试了它。
也可以修改我在此处创建的 PathBindable
以改为使用底层 String
,并接受特定的日期格式。
package com.example.libs
import java.util.Date
import java.text.SimpleDateFormat
import play.api.mvc.PathBindable
import scala.util.{Either, Failure, Success, Try}
object PathBinders {
implicit def bindableDate(implicit stringBinder: PathBindable[String]) = new PathBindable[Date] {
val sdf = new SimpleDateFormat("yyyy-MM-dd")
override def bind(key: String, value: String): Either[String, Date] = {
for {
dateString <- stringBinder.bind(key, value).right
date <- Try(sdf.parse(dateString)).toOption.toRight("Invalid date format.").right
} yield date
}
override def unbind(key: String, date: Date): String = key + "=" + sdf.format(date)
}
}
关于java - 在 Play Framework 中通过 GET 请求发送日期参数的理想方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30078615/