如何将大型变量数据从javascript发送到SpringMVC控制器

How to send large var Data from javascript to SpringMVC Controller?

本文关键字:SpringMVC 控制器 javascript 大型 变量 数据      更新时间:2023-09-26

我在springMVC工作。我面临一个问题,我的javascript函数具有json形式的大文本值,我想将数据从javascript发送到我的控制器 我该怎么做 有人帮助我 请检查我的代码

  function jsonString()
 {
   var jsonData ="{ description: "GM, DML Server Marketing",
                        email: "jeankend@name.com",
                        groupTitleColor: "#4169e1",
                        image: "d.png",
                        itemTitleColor: "#4b0082",
                        phone: "949-453-0415",
                        title: "Jean Kendall"
                    },
                    { description: "GM, Application Platform and Development Marketing",
                        email: "bradwhit@name.com",
                        groupTitleColor: "#4169e1",
                        image: "f.png",
                        itemTitleColor: "#4b0082",
                        phone: "502-528-6379",
                        title: "Brad Whitt"
                    }
 }"

就像我有大数据一样..现在我想使用 spring mvc 将此 json 格式数据存储在我的数据库中,如何将此数据从 javascript 发送到 Spring MVC 中的控制器我以这种方式购买,请检查我的代码 在我的控制器中

        public  @ResponseBody String saveOrgChart(@PathVariable String domainName,@RequestParam String domainId,@RequestBody String  jsonStr)
      {
    logger.warning("the domainName is..:"+domainName);
    logger.warning("the domainId is....:"+domainId);
    logger.warning("the jsonStirng is.."+jsonStr);
    Long primaryDomainId = 0L;
    }

它是否正确或有任何其他方法?请告诉我

不要使用字符串...您生活在对象世界中,请使用对象。创建一个可以保存数据的对象,而不是纯字符串,将其作为 JSON 对象数组发送。数组可以作为元素的列表/集合进行检索。

public class Person {
    private String description;
    private String email;
    private String groupTitleColor;
    private String image;
    private String itemTitleColor;
    private String phone;
    private String title;
    // Getters/Setters omitted
}

现在,您的控制器可能如下所示

public @ResponseBody String saveOrgChart(@PathVariable String domainName,@RequestParam String domainId,@RequestBody List persons) { ... }

尽管您可能需要将 List 包装在另一个容器对象中。