在Play Framework中通过GET请求发送日期参数的理想方式是什么? [英] What is the ideal way to send a date parameter over a GET request in Play Framework?
问题描述
当尝试在路由中执行此操作时文件,
GET / food / fetchMealInfo /:noOfDays /:dateSelected controllers.trackandplan.FoodController.fetchMealInfo(noOfDays:Integer,dateSelected :日期)
我收到错误说
没有找到编译错误
:类型日期
将前端AngularJS应用程序中的日期对象从Play Framework中传递到Java应用程序的正确,安全和干净的方法是什么?
请指导。
你有几个选项。稍微更容易理解的方法是简单地将日期/时间作为 Long
(unix timestamp)发送,并将其转换为 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
的接口,我们不能)。
应用/ 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(_))
}
覆盖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
导入scala.util {否,失败,成功,尝试}
对象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(日期格式无效)。右
}收益日期
}
override def unbind(key:String,date:Date):String = key +=+ sdf.format(date)
}
}
I am new to Play Framework. I am able to send simple data types like string, integer etc directly via the request and access them in the Back end Java method.
When I try doing this in the route file,
GET /food/fetchMealInfo/:noOfDays/:dateSelected controllers.trackandplan.FoodController.fetchMealInfo(noOfDays : Integer, dateSelected : Date)
I am getting an error saying
Compilation error
not found: type Date
What is the correct, safe and clean way to transfer a date object from a front end AngularJS application to the Java application in Play Framework. Please guide.
You have a couple options. The slightly easier way to understand is to simply transmit the date/time as a Long
(unix timestamp), and convert it to a Date
in the controller method.
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());
...
}
The more sophisticated way would be to use a PathBindable
, which would allow you to use Date
within the routes file itself. However, you would still need to transmit the Date
as a Long
(the PathBindable
would make the conversion if possible). Unfortunately, since we obviously don't have control over Date
, we have to implement PathBindable
in Scala, and not Java (Java would require implementing an interface for Date
, which we can't).
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
}
}
In order for the routes file to be able to pick this up, you'll need to add the following to your build.sbt
file:
PlayKeys.routesImport += "com.example.libs.PathBinders._"
PlayKeys.routesImport += "java.util.Date"
Now you can use Date
within your routes file (as Long
), without the need to handle it specially for every method that uses it.
GET /food/fetchMealInfo/:noOfDays/:dateSelected controllers.trackandplan.FoodController.fetchMealInfo(noOfDays: Integer, dateSelected: Date)
Note: This might not compile straight away if you're using an older Play version. I tested it with Play 2.3.8 and sbt 0.13.5.
It is also possible to modify the PathBindable
I made here to use an underlying String
instead, and accept a specific date format.
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)
}
}
这篇关于在Play Framework中通过GET请求发送日期参数的理想方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!