Request.bodyParam() return null and cause stacktrace
See original GitHub issueHi ! I cloned your sample tipsy/javalin-kotlin-example to try your technology which I found very interesting !
Explanations
After the repo was cloned, I imported the project on IntelliJ (ultimate edition). I ran the project from the Main.kt
class and I tried some of the API routes.
All the routes using the GET method work totally fine but the routes which need to parse the request body trigger a stacktrace in the console and the creation or update is aborted.
Those routes doesn’t work :
http://localhost:7000/users/create
http://localhost:7000/users/update/:id
After some debugging, I found that the method bodyParam()
in the Request
class return null.
Here an example of a request which does not work :
- route:
http://localhost:7000/users/create
- method:
POST
- body:
{ "name":"Michel", "email":"michel@test.com" }
When I do println(req.body())
at the top of the method, I get this :
{
"name": "Michel",
"email": "michel@alice.kt"
}
So, the body is well retrieved in the request !
But when I do println("name="+req.bodyParam("name")+", email="+req.bodyParam("email"))
, I get :
name=null, email=null
Stacktraces
Here the first stacktrace when we use the alias Request.bp(key: String)
setted up at the bottom of the Main.kt file :
java.lang.IllegalStateException: this.bodyParam(key) must not be null
at app.MainKt.bp(Main.kt:59)
at app.MainKt$main$$inlined$with$lambda$4.handle(Main.kt:28)
at io.javalin.core.JavalinServlet.service(JavalinServlet.java:62)
at io.javalin.embeddedserver.jetty.JettyHandler.doHandle(JettyHandler.java:35)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1568)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:564)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:317)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
at org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:128)
at org.eclipse.jetty.util.thread.Invocable$InvocableExecutor.invoke(Invocable.java:222)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:294)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:199)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:672)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:590)
at java.lang.Thread.run(Thread.java:745)
Then, this is the stacktrace when we use the bodyParam()
method :
java.lang.IllegalStateException: req.bodyParam("name") must not be null
at app.MainKt$main$$inlined$with$lambda$4.handle(Main.kt:28)
at io.javalin.core.JavalinServlet.service(JavalinServlet.java:62)
at io.javalin.embeddedserver.jetty.JettyHandler.doHandle(JettyHandler.java:35)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1568)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:564)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:317)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
at org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:128)
at org.eclipse.jetty.util.thread.Invocable$InvocableExecutor.invoke(Invocable.java:222)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:294)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:199)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:672)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:590)
at java.lang.Thread.run(Thread.java:745)
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
Okay nice work ! I’ll try to use Javalin with a database later to compare it with Spring MVC ! Good continuation on the project 😉
Javalin has an optional dependency on Jackson to do
req.bodyAsClass()
andres.json()
. In this specific example I’m using jackson-kotlin: