Javalin,无法直接从 HttpServlet 获取请求主体
Javalin, cannot get request body , except from HttpServlet directly
我无法从 Javalin 的 context
对象获取请求正文
像 context.formParam
,context.body()
,context.bodyAsClass()
我试过这样,我可以得到
请求体,我直接用HttpServletRequest
获取
请求正文
public void addPerson(Context ctx){
Map<String, Object> map = new HashMap<>();
try {
String name = ctx.req.getParameter("person_name");
String address = ctx.req.getParameter("person_address");
personList.add(new Person(name,address));
map.put("status",200);
map.put("data","Success ");
ctx.json(map);
}catch (Exception ex){
map.put("status",500);
map.put("data","Fail");
ctx.status(500).json(map);
}
}
如果我将 ctx.req.getParameter
更改为 ctx.formBody
服务器的日志中会有错误消息
Exception occurred while servicing http-request
java.lang.NoSuchMethodError: kotlin.io.ByteStreamsKt.readBytes(Ljava/io/InputStream;)[B
at io.javalin.http.CachedRequestWrapper$cachedBytes.invoke(CachedRequestWrapper.kt:21) ~[javalin-3.0.0.jar:na]
at io.javalin.http.CachedRequestWrapper$cachedBytes.invoke(CachedRequestWrapper.kt:16) ~[javalin-3.0.0.jar:na]
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74) ~[kotlin-stdlib-1.2.71.jar:1.2.71-release-64 (1.2.71)]
at io.javalin.http.CachedRequestWrapper.getCachedBytes(CachedRequestWrapper.kt) ~[javalin-3.0.0.jar:na]
at io.javalin.http.CachedRequestWrapper.getInputStream(CachedRequestWrapper.kt:31) ~[javalin-3.0.0.jar:na]
at io.javalin.http.Context.bodyAsBytes(Context.kt:118) ~[javalin-3.0.0.jar:na]
at io.javalin.http.Context.body(Context.kt:108) ~[javalin-3.0.0.jar:na]
at...
看来您使用的是旧版本的 Kotlin,您应该升级到 1。3.X。
我无法从 Javalin 的 context
对象获取请求正文
像 context.formParam
,context.body()
,context.bodyAsClass()
我试过这样,我可以得到
请求体,我直接用HttpServletRequest
获取
请求正文
public void addPerson(Context ctx){
Map<String, Object> map = new HashMap<>();
try {
String name = ctx.req.getParameter("person_name");
String address = ctx.req.getParameter("person_address");
personList.add(new Person(name,address));
map.put("status",200);
map.put("data","Success ");
ctx.json(map);
}catch (Exception ex){
map.put("status",500);
map.put("data","Fail");
ctx.status(500).json(map);
}
}
如果我将 ctx.req.getParameter
更改为 ctx.formBody
服务器的日志中会有错误消息
Exception occurred while servicing http-request
java.lang.NoSuchMethodError: kotlin.io.ByteStreamsKt.readBytes(Ljava/io/InputStream;)[B
at io.javalin.http.CachedRequestWrapper$cachedBytes.invoke(CachedRequestWrapper.kt:21) ~[javalin-3.0.0.jar:na]
at io.javalin.http.CachedRequestWrapper$cachedBytes.invoke(CachedRequestWrapper.kt:16) ~[javalin-3.0.0.jar:na]
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74) ~[kotlin-stdlib-1.2.71.jar:1.2.71-release-64 (1.2.71)]
at io.javalin.http.CachedRequestWrapper.getCachedBytes(CachedRequestWrapper.kt) ~[javalin-3.0.0.jar:na]
at io.javalin.http.CachedRequestWrapper.getInputStream(CachedRequestWrapper.kt:31) ~[javalin-3.0.0.jar:na]
at io.javalin.http.Context.bodyAsBytes(Context.kt:118) ~[javalin-3.0.0.jar:na]
at io.javalin.http.Context.body(Context.kt:108) ~[javalin-3.0.0.jar:na]
at...
看来您使用的是旧版本的 Kotlin,您应该升级到 1。3.X。