我想在以下代码中将message
类型scala.Seq[Scala.Document]
为 JSON 格式的变量转换为:
path("getMessages"){ get { parameters('roomname.as[String]) { (roomname) => try { val messagesByGroupName = MongoDatabase.collectionForChat.find(equal("groupChatName",roomname)).toFuture() val messages = Await.result(messagesByGroupName,60.seconds) println("Messages:"+messages) complete(messages) } catch { case e:TimeoutException => complete("Reading file timeout.") } } }
但它在完成(消息)行上给了我错误。它不接受该类型的消息。
我尝试使用以下方法将其转换为 JSON:
import play.api.libs.json._ object MyJsonProtocol{ implicit object ChatFormat extends Format[Chat] { def writes(c: Chat) : JsValue = { val chatSeq = Seq ( "sender" -> JsString(c.sender), "receiver" -> JsString(c.receiver), "message" -> JsString(c.message), "groupChatName" -> JsString(c.groupChatName), ) JsObject(chatSeq) } def reads(value: JsValue) = { JsSuccess(Chat("","","","")) } } }
但它不起作用。
我的Chat.scala课程如下:
import play.api.libs.json.{Json, Reads, Writes} class Chat(var sender:String,var receiver:String,var message:String, var groupChatName:String){ def setSenderName(senderName:String) = { sender = senderName } def setReceiverName(receiverName:String) = { receiver = receiverName } def setMessage(getMessage:String) = { message = getMessage } def setGroupChatName(chatName:String) = { groupChatName = chatName } } object Chat { def apply(sender: String, receiver: String, message: String, groupname: String): Chat = new Chat(sender, receiver, message,groupname) def unapply(arg: Chat): Option[(String, String, String,String)] = ??? implicit val requestReads: Reads[Chat] = Json.reads[Chat] implicit val requestWrites: Writes[Chat] = Json.writes[Chat] }
我也无法弄清楚在unapply方法中要写什么。我是 scala 和 akka 的新手。
编辑: 我的MongoDatabase.scala集合如下:
object MongoDatabase { val chatCodecProvider = Macros.createCodecProvider[Chat]() val codecRegistry = CodecRegistries.fromRegistries( CodecRegistries.fromProviders(chatCodecProvider), DEFAULT_CODEC_REGISTRY ) implicit val system = ActorSystem("Scala_jwt-App") implicit val executor: ExecutionContext = system.dispatcher val mongoClient: MongoClient = MongoClient() val databaseName = sys.env("database_name") // Getting mongodb database val database: MongoDatabase = mongoClient.getDatabase(databaseName).withCodecRegistry(codecRegistry) val registrationCollection = sys.env("register_collection_name") val chatCollection = sys.env("chat_collection") // Getting mongodb collection val collectionForUserRegistration: MongoCollection[Document] = database.getCollection(registrationCollection) collectionForUserRegistration.drop() val collectionForChat: MongoCollection[Document] = database.getCollection(chatCollection) collectionForChat.drop() }
如果尝试改变 val collectionForChat: MongoCollection[Document] = database.getCollection(chatCollection)
至
val collectionForChat: MongoCollection[Chat] = database.getCollection[Chat](chatCollection)
然后我在下面的saveChatMessage()方法中出现错误:
def saveChatMessage(sendMessageRequest: Chat) : String = { val senderToReceiverMessage : Document = Document( "sender" -> sendMessageRequest.sender, "receiver" -> sendMessageRequest.receiver, "message" -> sendMessageRequest.message, "groupChatName" -> sendMessageRequest.groupChatName) val chatAddedFuture = MongoDatabase.collectionForChat.insertOne(senderToReceiverMessage).toFuture() Await.result(chatAddedFuture,60.seconds) "Message sent" }
在val chatAddedFuture = MongoDatabase.collectionForChat.insertOne(senderToReceiverMessage).toFuture()
这条线上,因为它接受Seq[Document]类型的数据,我正在尝试添加Seq[Chat]类型的数据