本文共 4302 字,大约阅读时间需要 14 分钟。
之前我们已经把基层架构搭建好了,那么要如何使用呢?
首先看看我的Controller层代码@Singletonclass BMAuthController @Inject()(implicit cc: ControllerComponents, actorSystem: ActorSystem) extends AbstractController(cc) with Circe with CirceJsonapiSupport { val entry = PlayEntry() def parseJson(jsonString: String) : Json = io.circe.parser.parse(jsonString).right.get def decodeJson[T](json: Json)(implicit d: io.circe.Decoder[T]) : T = json.as[T].right.get def login = Action(circe.json[RootObject]) { implicit request => request import model.request.requestsJsonApiOpt.requestsJsonapiRootObjectReader._ val tt = fromJsonapi(request.body) val reVal = entry.commonExcution( SequenceSteps(testStep(tt.reqs.head) :: Nil, None)) val ctest = company("12", "alfred") val ctestj = asJsonApi(ctest) println(ctestj) val result = asJsonApiResult(reVal.asInstanceOf[userdetailresult]) Ok(result.asJson) }}
@Inject()(implicit cc: ControllerComponents, actorSystem: ActorSystem)
首先这个@Inject会查询Play的System环境,并将查到的==单例实例==注入到参数中.
官网解释
- AbstractController: an abstract class extending BaseController with a ControllerComponents constructor parameter that can be injected using constructor injection.
也就是说,这就是BaseController的抽象子类,但是要带有一个ControllerComponents作为构造函数.
circe是一个Scala的Json解析库.并且目前已经支持Play.
Play的使用方式很简单,首先引入项目:libraryDependencies += "com.dripower" %% "play-circe" % "2609.1"
然后继承play.api.libs.circe.Circe特质
使用起来也很简单,上面代码的
circe.json[RootObject]
部分就是在使用Circe解析JsonApi的Root部分. 对JsonApi协议的支持,里面主要就是两个隐式,代码如下:
trait CirceJsonapiSupport extends CirceJsonapiEncoders with CirceJsonapiDecoders { implicit val circeJsonapiMarshaller = Marshaller.delegate[RootObject, String]( `application/vnd.api+json`, `application/json`, ContentTypes.`application/json` )(_.asJson.noSpaces) implicit val circeJsonapiUnmarshaller = Unmarshaller.delegate[String, RootObject]( `application/vnd.api+json`, `application/json` )(decode[RootObject](_).right.get)}object CirceJsonapiSupport extends CirceJsonapiSupport
对不起,各位,暂时没写!!! ,有兴趣的可以私信我,后期我会补上.
学自
今天要说的当然不是大家熟知的构造函数,而是以前我们可能忽略的细节.
class User1(var id: String, var name: String)class User2(val id: String, val name: String)class User3(private var id: String,private var name: String)class User4(id: String, name: String)
上面代码定义了四个User类,每个类都有两个参数id
和name
,当然,他们构造函数的区别也很明显.那么这几种不同的定义方式,有什么区别呢?
user1.id
,或者赋值user1.id("001")
user2.id
,无法赋值private[this] val
,也就是说对于自己和伴生类而言是可见的应杨总要求,我们打印上面四个User类的编译信息
def tree1 = reify { class User1(var id: String, var name: String) }.treedef tree2 = reify { class User2(val id: String, val name: String) }.treedef tree3 = reify { class User3(private var id: String,private var name: String) }.treedef tree4 = reify { class User4(id: String, name: String) }.tree
然后分别打印上面的四个树,输出结果如下:
tree1:
{ class User1 extends AnyRef {var id: Predef.String = _; var name: Predef.String = _; def (id: Predef.String, name: Predef.String) = { super. (); () } }; ()}
tree2:
{ class User2 extends AnyRef {val id: Predef.String = _; val name: Predef.String = _; def (id: Predef.String, name: Predef.String) = { super. (); () } }; ()}
tree3:
{ class User3 extends AnyRef {private var id: Predef.String = _; private var name: Predef.String = _; def (id: Predef.String, name: Predef.String) = { super. (); () } }; ()}
tree4:
{ class User3 extends AnyRef {private[this] val id: Predef.String = _; private[this] val name: Predef.String = _; def (id: Predef.String, name: Predef.String) = { super. (); () } }; ()}
转载地址:http://efjko.baihongyu.com/