如何读取Client通过Spark发送的数据? [英] How read data sent by Client with Spark?

查看:133
本文介绍了如何读取Client通过Spark发送的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须读取客户端使用Spark(Java框架)发送的一些数据.

I have to read some data sent by Client using Spark (a framework for Java).

这是客户发帖请求的代码.我正在使用jQuery.

This is the code of client's post request. I am using jQuery.

$.post("/insertElement", 
{item:item.value, value: value.value, dimension: dimension.value });

服务器代码:

post(new Route("/insertElement") {
        @Override
        public Object handle(Request request, Response response) {
            String item = (String) request.attribute("item");
            String value = (String) request.attribute("value");
            String dimension = (String) request.attribute("dimension");
            Element e = new Element(item, value, dimension);
            ElementDAO edao = new ElementDAO();
            edao.insert(e);
            JSONObject json = JSONObject.fromObject( e );
            return json; 
        }
    });

我正在使用Spark,因此我只需要定义路线. 我想将客户端发送的数据存储在数据库中,但是所有属性都为空.

I am using Spark so I only have to define the route. I would like to store in a database the data sent by client, but all the attributes are null.

我认为这种方式是不正确的.如何读取发送的数据?

I think that this way isn't correct. How can I read the sent data?

推荐答案

使用HTTP POST发送数据的方式是将JSON作为请求 body 发布,而不是作为请求发布属性.这意味着您不应该使用request.attribute("item")和其他名称,而应将请求正文解析为Java对象.您可以使用该对象创建element并使用DAO进行存储.

They way you send your data, using HTTP POST, you're posting the JSON as request body, not as request attributes. This means you shouldn't use request.attribute("item") and the others, but instead parse the request body to a Java object. You can use that object to create the element and store it using the DAO.

这篇关于如何读取Client通过Spark发送的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆