I'm trying to create a post request for insert a data to mongoddb using : 1. sbt 0.13.6 2. play 2.10 3. scala 2.11.2 4. play2-reactivamongo 0.10.2 5. mongodb 2.6.4
data post by json, and create a case class for the model, and using JSPath to convert the json to entity class.
this is my sample code :
def inserTransaction = Action(parser.json) { implicit request =>
val json = request.body
val data = json.as[Transaction]
Logger.info(data.toString)
val future = collection.insert(data.copy(id = Option[BSONObjectID](BSONObjectID.generate)))
var result = ""
future.onComplete {
case Failure(t) => result = "An error has occured: " + t.getMessage
case Success(post) => result = "success"
}
Ok(result)
}
I've seen some sample code that used Action.sync for handling asynchronous in controllers, but when i try to use Action.sync, my Intellij IDE detect an error "cannot resolve Action.sync as signature", i've tried to change the result of function like this
future.onComplete {
case Failure(t) => Ok("An error has occured: " + t.getMessage)
case Success(post) => Ok("success")
}
So I decided to use Action(parser.json) , but the issue that came from activator play is tell me that i should use "import play.api.libs.concurrent.Execution.Implicits._" in my code. But when i import the libraries, it came a new error :
! Internal server error, for (POST) [/insertdata] ->
java.lang.ExceptionInInitializerError: null ....
Caused by: play.api.PlayException: ReactiveMongoPlugin Error[The ReactiveMongoPlugin has not been
initialized! Please edit your conf/play.plugins file and add the following line....
when i tried to reload the request, it showed another error :
! Internal server error, for (POST) [/api/insertdata] ->
java.lang.NoClassDefFoundError: Could not initialize class controllers.TransactionController$
[error] application - Error while rendering default error page
scala.MatchError: java.lang.NoClassDefFoundError: Could not initialize class
controllers.TransactionController$ (of class java.lang.NoClassDefFoundError)
Anyone have any solution for my problem?
This question might help: Are there any benefits in using non-async actions in Play Framework 2.2?
Action vs Action.async
If you think of actions at high level that take and input and produce an output then these two do the same thing but slightly different. Both of these take in request and output results. Action.async allows the action code to properly deal with Futures. I am skipping over some details but I hope this helps.
Can you paste your plugins file?