- 浏览: 2552195 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Spray(6)REST API Project - JSON Marshall and Test Class
3. How to Write JSON Marshall Class
Enumeration class
package com.sillycat.easysprayrestserver.model
object UserType extends Enumeration {
type UserType = Value
valADMIN, CUSTOMER, SELLER = Value
}
object CartType extends Enumeration {
type CartType = Value
valDIRECT, CHENGDU = Value
}
Case Class
Here is the model class, which should be my POJO, case class in scala
package com.sillycat.easysprayrestserver.model
import org.joda.time.DateTime
caseclass Cart(id: Option[Long], cartName: String, cartType: CartType.Value, user: User, products: Seq[Product])
caseclass Product(id: Option[Long], productName: String, productDesn: String, createDate: DateTime, expirationDate: DateTime)
caseclass User(id: Option[Long], userName: String, age: Int, userType: UserType.Value, createDate: DateTime, expirationDate: DateTime)
Here is the implementation of JSON Marshall
package com.sillycat.easysprayrestserver.model
import spray.httpx.SprayJsonSupport
import spray.json.DefaultJsonProtocol
import spray.json.DeserializationException
import spray.json.JsNumber
import spray.json.JsObject
import spray.json.JsString
import spray.json.JsValue
import spray.json.RootJsonFormat
import org.joda.time.format.DateTimeFormat
import org.joda.time.DateTime
import spray.json.JsArray
import spray.json._
import DefaultJsonProtocol._
class UserJsonProtocol(currentId: Long) extends DefaultJsonProtocol {
privatevaldateTimeFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm")
implicitobject UserJsonFormat extends RootJsonFormat[User] {
def write(user: User) = JsObject(
Map(
"userName" -> JsString(user.userName),
"age" -> JsNumber(user.age),
"userType" -> JsString(user.userType.toString()),
"createDate" -> JsString(dateTimeFormat.print(new DateTime(user.createDate))),
"expirationDate" -> JsString(dateTimeFormat.print(new DateTime(user.expirationDate)))
) ++
user.id.map( i => Map("id" -> JsNumber(i))).getOrElse(Map[String, JsValue]())
)
def read(jsUser: JsValue) = {
jsUser.asJsObject.getFields("id", "userName", "age", "userType", "createDate", "expirationDate") match {
case Seq(JsNumber(id), JsString(userName), JsNumber(age), JsString(userType), JsString(createDate), JsString(expirationDate)) =>
valcreateDateObject = dateTimeFormat.parseDateTime(createDate)
valexpirationDateObject = dateTimeFormat.parseDateTime(expirationDate)
new User(Some(id.longValue), userName, age.toInt, UserType.withName(userType), createDateObject, expirationDateObject)
case Seq(JsString(userName), JsNumber(age), JsString(userType), JsString(createDate), JsString(expirationDate)) =>
valcreateDateObject = dateTimeFormat.parseDateTime(createDate)
valexpirationDateObject = dateTimeFormat.parseDateTime(expirationDate)
new User( None, userName, age.toInt, UserType.withName(userType), createDateObject, expirationDateObject)
case _ => thrownew DeserializationException("User expected")
}
}
}
}
object ProductJsonProtocol extends DefaultJsonProtocol {
privatevaldateTimeFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm")
implicitobject ProductJsonFormat extends RootJsonFormat[Product] {
def write(product: Product) = JsObject(
Map(
"productName" -> JsString(product.productName),
"productDesn" -> JsString(product.productDesn),
"createDate"-> JsString(dateTimeFormat.print(new DateTime(product.createDate))),
"expirationDate" -> JsString(dateTimeFormat.print(new DateTime(product.expirationDate)))
) ++
product.id.map( i => Map("id" -> JsNumber(i))).getOrElse(Map[String, JsValue]())
)
def read(jsProduct: JsValue) = {
jsProduct.asJsObject.getFields("id", "productName", "productDesn", "createDate", "expirationDate") match {
case Seq(JsNumber(id), JsString(productName), JsString(productDesn), JsString(createDate), JsString(expirationDate)) =>
valcreateDateObject = dateTimeFormat.parseDateTime(createDate)
valexpirationDateObject = dateTimeFormat.parseDateTime(expirationDate)
new Product(Some(id.longValue), productName, productDesn, createDateObject, expirationDateObject)
case Seq(JsString(productName), JsString(productDesn), JsString(createDate), JsString(expirationDate)) =>
valcreateDateObject = dateTimeFormat.parseDateTime(createDate)
valexpirationDateObject = dateTimeFormat.parseDateTime(expirationDate)
new Product(None, productName, productDesn, createDateObject, expirationDateObject)
case _ => thrownew DeserializationException("Product expected")
}
}
}
}
object CartJsonProtocol extends DefaultJsonProtocol {
implicitobject CartJsonFormat extends RootJsonFormat[Cart] {
implicitvaluserFormatter = (new UserJsonProtocol(1)).UserJsonFormat
implicitvalproductFormatter = ProductJsonProtocol.ProductJsonFormat
def write(cart: Cart) = JsObject(
Map(
"cartName" -> JsString(cart.cartName),
"cartType" -> JsString(cart.cartType.toString()),
"user" -> cart.user.toJson,
"products" -> cart.products.toJson
) ++
cart.id.map( i => Map("id" -> JsNumber(i))).getOrElse(Map[String, JsValue]())
)
def read(jsCart: JsValue) = {
valparams: Map[String, JsValue] = jsCart.asJsObject.fields
Cart(
params.get("id").map(_.convertTo[Int]),
params("cartName").convertTo[String],
CartType.withName(params("cartType").convertTo[String]),
params("user").convertTo[User],
params("products").convertTo[Seq[Product]]
)
}
}
}
4. How to Write Test
First of all, I put the package dependency in build.sbt.
"org.scalatest" % "scalatest_2.10" % "1.9.1" % "test",
"org.specs2" %% "specs2" % "1.13" % "test"
And here is a example for JSONMarchall class
package com.sillycat.easysprayrestserver.model
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
import spray.json.DefaultJsonProtocol
import spray.json.pimpAny
import spray.json.pimpString
class JSONMarshallSpec extends FlatSpec with ShouldMatchers with DefaultJsonProtocol {
import com.sillycat.easysprayrestserver.model.ProductJsonProtocol._
import com.sillycat.easysprayrestserver.model.CartJsonProtocol._
"Marshalling Product JSON" should "result in an Product Object" in {
valjsonProduct = """{
"id" : 1,
"productName" : "iphone 5",
"productDesn" : "A good mobile device.",
"createDate" : "2012-05-22 13:33",
"expirationDate" : "2012-05-22 14:33"
}"""
valproductAST = jsonProduct.asJson
info("ProductAST: " + productAST.asJsObject)
valproduct: Product = productAST.convertTo[Product]
info("Product Object: " + product.toJson)
assert(product.toJson === productAST)
}
"Marshalling User without ID JSON" should "result in an User Object without id" in {
valjsonUser = """{
"userName":"Carl",
"age": 31,
"userType": "ADMIN",
"createDate": "2012-05-21 12:34",
"expirationDate": "2013-05-12 12:34"
}"""
implicitvalformatter = (new UserJsonProtocol(1)).UserJsonFormat
valuserAST = jsonUser.asJson
info("UserAST: " + userAST.prettyPrint)
valuser: User = userAST.convertTo[User]
info("User Object: " + user.toJson)
assert(user.age === 31)
assert(user.id === None)
}
Run the test
>sbt test
sbt>test-only com.sillycat.easysprayrestserver.model.*
sbt>test-only com.sillycat.easysprayrestserver.model.JSONMarshallSpec
That is it. I works fine.
5. How to Work with Actor
come soon…
6. How to Work with Logback
come soon…
Tips
Error Message:
error while loading AbstractInstant
Solution:
Add some dependency to build.sbt file:
"joda-time" % "joda-time" % "2.2",
"org.joda" % "joda-convert" % "1.3.1"
Error Message:
sbt.ResolveException: download failed: com.chuusai#shapeless_2.10.0-RC1;1.2.3-SNAPSHOT!shapeless_2.10.0-RC1.jar
[warn] ==== sonatype snapshots: tried
[warn] https://oss.sonatype.org/content/repositories/snapshots/com/chuusai/shapeless_2.10.0-RC1/1.2.3-SNAPSHOT/shapeless_2.10.0-RC1-1.2.3-SNAPSHOT.jar
Solution:
>git clone https://github.com/milessabin/shapeless.git
>cd shapeless
>git branch -a
remotes/origin/shapeless-1.2.3
>git checkout shapeless-1.2.3
>sbt package
Even this project is not working.
I try to download this jar from google search result, but I can not find one.
At last I copy that from my colleague's computer and put it in this place>
/Users/carl/.ivy2/cache/com.chuusai/shapeless_2.10.0-RC1/jars/shapeless_2.10.0-RC1-1.2.3-SNAPSHOT.jar
Error Message:
Could not run test com.sillycat.easysprayrestserver.model.JSONMarshallSpec:
java.lang.NoClassDefFoundError: scala/math/ScalaNumericAnyConversions
Solution:
Add 2 dependencies in build.sbt
"org.scalatest" % "scalatest_2.10" % "1.9.1" % "test",
"org.specs2" %% "specs2" % "1.13" % "test"
References:
http://spray.io/documentation/spray-testkit/
http://etorreborre.github.io/specs2/guide/org.specs2.guide.Runners.html#Via+SBT
3. How to Write JSON Marshall Class
Enumeration class
package com.sillycat.easysprayrestserver.model
object UserType extends Enumeration {
type UserType = Value
valADMIN, CUSTOMER, SELLER = Value
}
object CartType extends Enumeration {
type CartType = Value
valDIRECT, CHENGDU = Value
}
Case Class
Here is the model class, which should be my POJO, case class in scala
package com.sillycat.easysprayrestserver.model
import org.joda.time.DateTime
caseclass Cart(id: Option[Long], cartName: String, cartType: CartType.Value, user: User, products: Seq[Product])
caseclass Product(id: Option[Long], productName: String, productDesn: String, createDate: DateTime, expirationDate: DateTime)
caseclass User(id: Option[Long], userName: String, age: Int, userType: UserType.Value, createDate: DateTime, expirationDate: DateTime)
Here is the implementation of JSON Marshall
package com.sillycat.easysprayrestserver.model
import spray.httpx.SprayJsonSupport
import spray.json.DefaultJsonProtocol
import spray.json.DeserializationException
import spray.json.JsNumber
import spray.json.JsObject
import spray.json.JsString
import spray.json.JsValue
import spray.json.RootJsonFormat
import org.joda.time.format.DateTimeFormat
import org.joda.time.DateTime
import spray.json.JsArray
import spray.json._
import DefaultJsonProtocol._
class UserJsonProtocol(currentId: Long) extends DefaultJsonProtocol {
privatevaldateTimeFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm")
implicitobject UserJsonFormat extends RootJsonFormat[User] {
def write(user: User) = JsObject(
Map(
"userName" -> JsString(user.userName),
"age" -> JsNumber(user.age),
"userType" -> JsString(user.userType.toString()),
"createDate" -> JsString(dateTimeFormat.print(new DateTime(user.createDate))),
"expirationDate" -> JsString(dateTimeFormat.print(new DateTime(user.expirationDate)))
) ++
user.id.map( i => Map("id" -> JsNumber(i))).getOrElse(Map[String, JsValue]())
)
def read(jsUser: JsValue) = {
jsUser.asJsObject.getFields("id", "userName", "age", "userType", "createDate", "expirationDate") match {
case Seq(JsNumber(id), JsString(userName), JsNumber(age), JsString(userType), JsString(createDate), JsString(expirationDate)) =>
valcreateDateObject = dateTimeFormat.parseDateTime(createDate)
valexpirationDateObject = dateTimeFormat.parseDateTime(expirationDate)
new User(Some(id.longValue), userName, age.toInt, UserType.withName(userType), createDateObject, expirationDateObject)
case Seq(JsString(userName), JsNumber(age), JsString(userType), JsString(createDate), JsString(expirationDate)) =>
valcreateDateObject = dateTimeFormat.parseDateTime(createDate)
valexpirationDateObject = dateTimeFormat.parseDateTime(expirationDate)
new User( None, userName, age.toInt, UserType.withName(userType), createDateObject, expirationDateObject)
case _ => thrownew DeserializationException("User expected")
}
}
}
}
object ProductJsonProtocol extends DefaultJsonProtocol {
privatevaldateTimeFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm")
implicitobject ProductJsonFormat extends RootJsonFormat[Product] {
def write(product: Product) = JsObject(
Map(
"productName" -> JsString(product.productName),
"productDesn" -> JsString(product.productDesn),
"createDate"-> JsString(dateTimeFormat.print(new DateTime(product.createDate))),
"expirationDate" -> JsString(dateTimeFormat.print(new DateTime(product.expirationDate)))
) ++
product.id.map( i => Map("id" -> JsNumber(i))).getOrElse(Map[String, JsValue]())
)
def read(jsProduct: JsValue) = {
jsProduct.asJsObject.getFields("id", "productName", "productDesn", "createDate", "expirationDate") match {
case Seq(JsNumber(id), JsString(productName), JsString(productDesn), JsString(createDate), JsString(expirationDate)) =>
valcreateDateObject = dateTimeFormat.parseDateTime(createDate)
valexpirationDateObject = dateTimeFormat.parseDateTime(expirationDate)
new Product(Some(id.longValue), productName, productDesn, createDateObject, expirationDateObject)
case Seq(JsString(productName), JsString(productDesn), JsString(createDate), JsString(expirationDate)) =>
valcreateDateObject = dateTimeFormat.parseDateTime(createDate)
valexpirationDateObject = dateTimeFormat.parseDateTime(expirationDate)
new Product(None, productName, productDesn, createDateObject, expirationDateObject)
case _ => thrownew DeserializationException("Product expected")
}
}
}
}
object CartJsonProtocol extends DefaultJsonProtocol {
implicitobject CartJsonFormat extends RootJsonFormat[Cart] {
implicitvaluserFormatter = (new UserJsonProtocol(1)).UserJsonFormat
implicitvalproductFormatter = ProductJsonProtocol.ProductJsonFormat
def write(cart: Cart) = JsObject(
Map(
"cartName" -> JsString(cart.cartName),
"cartType" -> JsString(cart.cartType.toString()),
"user" -> cart.user.toJson,
"products" -> cart.products.toJson
) ++
cart.id.map( i => Map("id" -> JsNumber(i))).getOrElse(Map[String, JsValue]())
)
def read(jsCart: JsValue) = {
valparams: Map[String, JsValue] = jsCart.asJsObject.fields
Cart(
params.get("id").map(_.convertTo[Int]),
params("cartName").convertTo[String],
CartType.withName(params("cartType").convertTo[String]),
params("user").convertTo[User],
params("products").convertTo[Seq[Product]]
)
}
}
}
4. How to Write Test
First of all, I put the package dependency in build.sbt.
"org.scalatest" % "scalatest_2.10" % "1.9.1" % "test",
"org.specs2" %% "specs2" % "1.13" % "test"
And here is a example for JSONMarchall class
package com.sillycat.easysprayrestserver.model
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
import spray.json.DefaultJsonProtocol
import spray.json.pimpAny
import spray.json.pimpString
class JSONMarshallSpec extends FlatSpec with ShouldMatchers with DefaultJsonProtocol {
import com.sillycat.easysprayrestserver.model.ProductJsonProtocol._
import com.sillycat.easysprayrestserver.model.CartJsonProtocol._
"Marshalling Product JSON" should "result in an Product Object" in {
valjsonProduct = """{
"id" : 1,
"productName" : "iphone 5",
"productDesn" : "A good mobile device.",
"createDate" : "2012-05-22 13:33",
"expirationDate" : "2012-05-22 14:33"
}"""
valproductAST = jsonProduct.asJson
info("ProductAST: " + productAST.asJsObject)
valproduct: Product = productAST.convertTo[Product]
info("Product Object: " + product.toJson)
assert(product.toJson === productAST)
}
"Marshalling User without ID JSON" should "result in an User Object without id" in {
valjsonUser = """{
"userName":"Carl",
"age": 31,
"userType": "ADMIN",
"createDate": "2012-05-21 12:34",
"expirationDate": "2013-05-12 12:34"
}"""
implicitvalformatter = (new UserJsonProtocol(1)).UserJsonFormat
valuserAST = jsonUser.asJson
info("UserAST: " + userAST.prettyPrint)
valuser: User = userAST.convertTo[User]
info("User Object: " + user.toJson)
assert(user.age === 31)
assert(user.id === None)
}
Run the test
>sbt test
sbt>test-only com.sillycat.easysprayrestserver.model.*
sbt>test-only com.sillycat.easysprayrestserver.model.JSONMarshallSpec
That is it. I works fine.
5. How to Work with Actor
come soon…
6. How to Work with Logback
come soon…
Tips
Error Message:
error while loading AbstractInstant
Solution:
Add some dependency to build.sbt file:
"joda-time" % "joda-time" % "2.2",
"org.joda" % "joda-convert" % "1.3.1"
Error Message:
sbt.ResolveException: download failed: com.chuusai#shapeless_2.10.0-RC1;1.2.3-SNAPSHOT!shapeless_2.10.0-RC1.jar
[warn] ==== sonatype snapshots: tried
[warn] https://oss.sonatype.org/content/repositories/snapshots/com/chuusai/shapeless_2.10.0-RC1/1.2.3-SNAPSHOT/shapeless_2.10.0-RC1-1.2.3-SNAPSHOT.jar
Solution:
>git clone https://github.com/milessabin/shapeless.git
>cd shapeless
>git branch -a
remotes/origin/shapeless-1.2.3
>git checkout shapeless-1.2.3
>sbt package
Even this project is not working.
I try to download this jar from google search result, but I can not find one.
At last I copy that from my colleague's computer and put it in this place>
/Users/carl/.ivy2/cache/com.chuusai/shapeless_2.10.0-RC1/jars/shapeless_2.10.0-RC1-1.2.3-SNAPSHOT.jar
Error Message:
Could not run test com.sillycat.easysprayrestserver.model.JSONMarshallSpec:
java.lang.NoClassDefFoundError: scala/math/ScalaNumericAnyConversions
Solution:
Add 2 dependencies in build.sbt
"org.scalatest" % "scalatest_2.10" % "1.9.1" % "test",
"org.specs2" %% "specs2" % "1.13" % "test"
References:
http://spray.io/documentation/spray-testkit/
http://etorreborre.github.io/specs2/guide/org.specs2.guide.Runners.html#Via+SBT
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 476NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 337Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 436Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 385Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 478NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 424Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 248GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 452GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 328GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 314Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 319Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 294Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 312Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 288NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 261Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 574NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 266Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 368Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 370Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
AndroidAsync.zip,用于Android的异步套接字、HTTP(客户端 服务器)、WebSocket和socket.io库。基于nio,而不是threads.asynchronous socket、http(client server)和android的websocket库。基于nio,而不是线程。
"slick-pg_spray-json_2.10-0.5.2.2.zip"中的Slick-PG版本,结合了Scala的spray-json库,使得JSON数据可以无缝地在数据库和代码之间转换。spray-json是Scala的一个轻量级、快速且易于使用的JSON库,它可以方便地解析...
总的来说,spray-json 框架以其简洁的 API、高效的性能和对 Scala 语言特性的良好融合,为 JSON 数据的处理提供了强大且易用的解决方案。无论是在 Web 应用开发,还是在其他需要 JSON 格式数据处理的场景中,spray-...
spray-json-refined 一个集成了和给定JsonFormat[T]实例,此库派生JsonFormat[T Refined P]实例安装支持Scala版本: 2.13 , 2.12 将以下内容添加到您的build.sbt : libraryDependencies += "io.github.typeness" %...
总的来说,spray-json 是一个强大且灵活的 JSON 处理工具,它为 Scala 开发者提供了简洁的 API 和高效的转换能力,尤其适合处理基于 JSON 的数据交换场景。在实际项目中,结合 spray-json,可以轻松地构建前后端的...
spray-json-shapeless 自动派生 JsonFormat S,搭载(无宏被创建这个库的过程中写入)。 有关文档,请 ,有关示例,请。 购票前,请仔细阅读文档和示例。 TL; DR libraryDependencies + = " ...
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用