play REST hello 实例
所属分类 play
浏览量 741
路由定义 conf/routes
GET / controllers.HomeController.hello
GET /hello controllers.HomeController.hello
Controller
HomeController
package controllers;
import play.mvc.*;
import java.time.LocalDateTime;
public class HomeController extends Controller {
public Result hello() {
return ok("hello,"+ LocalDateTime.now());
}
}
启动运行
sbt run
-Dhttp.port=9100
http://127.0.0.1:9000
http://127.0.0.1:9000/hello
http://127.0.0.1:9000/xxx
xxx 路由未注册 ,会显示所有路由信息
idea 里 导入运行
run / edit configurations
添加一个 sbt task
tasks 里 输入 run
use sbt shell 不要勾选
动态获取参数
http://127.0.0.1:9000/hello2
/app/controllers/HelloController.scala
package controllers
import play.api.mvc._
import java.time.LocalDateTime
import javax.inject._
@Singleton
class HelloController @Inject()(cc: ControllerComponents) extends AbstractController(cc) {
def hello() = Action { implicit request: Request[AnyContent] =>
var name = request.getQueryString("name").getOrElse(null);
if(name==null){
name = "scala"
}
Ok("hello "+name+","+LocalDateTime.now())
}
}
完整代码
https://gitee.com/dyyx/hellocode/tree/master/demo/scala/play-rest-hello
上一篇
下一篇
maven 与 sbt 配置对比
获取当前shell脚本所在目录绝对路径
play框架简介
一些面试知识点整理
Scala快速入门
play scala slick example