Javalin,除了直接从HttpServlet之外,无法获取请求正文

时间:2019-06-16 06:08:02

标签: java spring web javalin

我无法从Javalin的context对象获取请求正文 像context.formParamcontext.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$2.invoke(CachedRequestWrapper.kt:21) ~[javalin-3.0.0.jar:na]
    at io.javalin.http.CachedRequestWrapper$cachedBytes$2.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...

1 个答案:

答案 0 :(得分:0)

您似乎使用的是Kotlin的旧版本,应该升级到1.3.X。