I am trying to store part of a model as Json in my DB. Ebean is supposed to have this helper :
@DbJson
public Map<String, Object> jsonContent;
But when I try to store my bean, it only works if I do not give it the jsonContent
field. As soon as I try to set it, I get the following error:
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[CompletionException: java.lang.ExceptionInInitializerError]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:251)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:178)
at play.core.server.AkkaHttpServer$$anonfun$2.applyOrElse(AkkaHttpServer.scala:343)
at play.core.server.AkkaHttpServer$$anonfun$2.applyOrElse(AkkaHttpServer.scala:341)
at scala.concurrent.Future.$anonfun$recoverWith$1(Future.scala:414)
at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)
at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:91)
at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
Caused by: java.util.concurrent.CompletionException: java.lang.ExceptionInInitializerError
at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273)
at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1592)
at java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1582)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Caused by: java.lang.ExceptionInInitializerError: null
at io.ebeaninternal.server.type.ScalarTypeJsonMap.formatValue(ScalarTypeJsonMap.java:166)
at io.ebeaninternal.server.type.ScalarTypeJsonMap.bind(ScalarTypeJsonMap.java:148)
at io.ebeaninternal.server.type.ScalarTypeJsonMap$Clob.bind(ScalarTypeJsonMap.java:55)
at io.ebeaninternal.server.deploy.BeanProperty.bind(BeanProperty.java:648)
at io.ebeaninternal.server.persist.dml.DmlHandler.bindInternal(DmlHandler.java:226)
at io.ebeaninternal.server.persist.dml.DmlHandler.bind(DmlHandler.java:198)
at io.ebeaninternal.server.persist.dmlbind.BindableProperty.dmlBind(BindableProperty.java:54)
at io.ebeaninternal.server.persist.dmlbind.BindableList.dmlBind(BindableList.java:62)
at io.ebeaninternal.server.persist.dml.InsertMeta.bind(InsertMeta.java:162)
at io.ebeaninternal.server.persist.dml.InsertHandler.bind(InsertHandler.java:97)
Caused by: java.lang.IllegalStateException: No service implementation found for SpiJsonService?
at io.ebean.text.json.EJson.init(EJson.java:31)
at io.ebean.text.json.EJson.<clinit>(EJson.java:23)
at io.ebeaninternal.server.type.ScalarTypeJsonMap.formatValue(ScalarTypeJsonMap.java:166)
at io.ebeaninternal.server.type.ScalarTypeJsonMap.bind(ScalarTypeJsonMap.java:148)
at io.ebeaninternal.server.type.ScalarTypeJsonMap$Clob.bind(ScalarTypeJsonMap.java:55)
at io.ebeaninternal.server.deploy.BeanProperty.bind(BeanProperty.java:648)
at io.ebeaninternal.server.persist.dml.DmlHandler.bindInternal(DmlHandler.java:226)
at io.ebeaninternal.server.persist.dml.DmlHandler.bind(DmlHandler.java:198)
at io.ebeaninternal.server.persist.dmlbind.BindableProperty.dmlBind(BindableProperty.java:54)
at io.ebeaninternal.server.persist.dmlbind.BindableList.dmlBind(BindableList.java:62)
[INFO] [08/06/2018 16:03:23.475] [Thread-2] [CoordinatedShutdown(akka://sbt-web)] Starting coordinated
My bean looks like this :
@Entity
@Table(name = "my_table")
public class MyBean extends AManageableModel {
public static final Finder<Long, Configuration> find = new Finder<>(MyBean.class);
@Constraints.Required
public String name;
@Constraints.Required
@JsonProperty("some_property")
public String someProperty;
@DbJson
@JsonProperty("json_content")
public Map<String, Object> jsonContent;
}
When I use the following Json to create a line in DB, it works (and jsonContent
is null
):
{
"name": "test 1",
"some_property": "some property content"
}
But when I use the following Json :
{
"name": "test 1",
"some_property": "some property content",
"json_content": {
"test":"test",
"test2":"test2"
}
}
it crashes (see beginning of the post for full stackTrace).
Serialization is done using Jackson :
Json.fromJson(jsonData, MyBean.class);
Okay, so using :
Map<String,String> jsonContent
instead of :
Map<String,Object> jsonContent
works for me, so I suspect "Object" needs to extends something that is serializable.
EDIT:
So, 2 years later I had to use Map<String,Object> jsonContent
for unreleated reason and the same problem came back. Here is a dirty solution, call this at applications startup:
try {
EJson.write(new Object());
} catch (IOException e1) {
e1.printStackTrace();
}
This forces EJson
to load and problem goes away.... but damn is it dirty.