【问题标题】:How read data sent by Client with Spark?如何用 Spark 读取 Client 发送的数据?
【发布时间】:2013-07-18 12:50:36
【问题描述】:

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

这是客户端发帖请求的代码。我正在使用 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,所以我只需要定义路线。 我想将客户端发送的数据存储在数据库中,但是所有属性都是空的。

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

【问题讨论】:

    标签: javascript jquery httprequest spark-java


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      你需要这样的东西:

      post(new Route("/insertElement") {
          @Override
          public Object handle(Request request, Response response) {
      
              String body = request.body();
              Element element = fromJson(body, Element.class);
              ElementDAO edao = new ElementDAO();
              edao.insert(e);
              JSONObject json = JSONObject.fromObject( e );
              return json; 
          }
      });
      
      
      public class Element {
      
          private String item;
          private String value;
          private String dimension;
      
          //constructor, getters and setters
      
      }
      
      public class JsonTransformer {
      
          public static String toJson(Object object) {
              return new Gson().toJson(object);
          }
      
          public static <T extends Object> T  fromJson(String json, Class<T> classe) {
              return new Gson().fromJson(json, classe);
          }
      
      }
      

      【讨论】:

      • 我认为最好有一个 Gson 全局实例以避免在每个请求上创建一个实例
      【解决方案3】:

      尝试使用 request.queryParams("item") 等等。

      【讨论】:

      • 这是用于 GET 请求的。
      【解决方案4】:

      假设这是我在请求中发送的 JSON

          { 'name': 'Rango' }
      

      这就是我将控制器配置为解析请求正文的方式。

          public class GreetingsController {
              GreetingsController() {
                  post("/hello", ((req, res) -> {
                      Map<String, String> map = JsonUtil.parse(req.body());
                      return "Hello " + map.get("name") + "!";
                  })));
              }
          }
      
          public class JsonUtil {
              public static Map<String, String> parse(String object) {
                  return new Gson().fromJson(object, Map.class);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-13
        相关资源
        最近更新 更多