繁体   English   中英

如何在 Springboot2 中将带有 JSON 主体的 POST API 调用转换为 java ZA2F2ED4F8DCEBC2CBB4DC21A?

[英]How to convert a POST API call with a JSON body to a java class in Springboot?

我正在使用包含配置详细信息的原始 JSON 主体传递 POST 请求,我想将其保存为 java class 以便我可以使用获取器将指定的详细信息分配给它们需要的相关类。我的请求

{
"aisles" : 2,
"sections" : 2,
"shelves" : 1,
"packagingAreas": [ "a1.3", "a2.3" ],
"workers" : [
    {
        "name" : "rem",
        "location" : "a1.1",
        "capacity" : 20
    }
],
"items" : [
    {
        "id" : "mars",
        "name" : "Mars",
        "supplier" : "Nestle",
        "weight" : 1
    },
    {
        "id" : "kitkat",
        "name" : "Kit Kat",
        "supplier" : "Nestle",
        "weight" : 1
    },
    {
        "id" : "dd",
        "name" : "Double Decker",
        "supplier" : "Nestle",
        "weight" : 1
    }
]
}

我想将该主体的详细信息放入我的配置中

public class Config {

    private static String aisles;
    private static String sections;
    private static String shelves;
    private static String packagingAreas[];
    private static ArrayList<Worker> workers;
    private static ArrayList<Item> items;

    public static String getAisles() {
        return aisles;
    }

    public static String getSections() {
        return sections;
    }

    public static String getShelves() {
        return shelves;
    }

    public static String[] getPackagingAreas() {
        return packagingAreas;
    }

    public static ArrayList<Worker> getWorkers() {
        return workers;
    }

    public static ArrayList<Item> getItems() {
        return items;
    }

}

我已经使用与 json 配置文件中相同的变量对工人和物品类进行了建模,有没有直接的方法可以将此 JSON 文件转换为 class? 如果不是我可以尝试什么其他方法? 提前致谢!

编辑-这是我创建的端点,使用@Rest Controller

@RequestMapping(value ="/config", method = RequestMethod.POST)
public void configure() {
   //i want to do the conversion here
  }
  1. 更新 controller 如下
@RequestMapping(value ="/config", method = RequestMethod.POST) 
public void configure(@RequestBody Config config) {
  //your json is converted to config java object
}
  1. 更新您的Config class
public class Config {
    private String aisles;
    private String sections;
    private String shelves;
    private String packagingAreas[];
    private ArrayList<Worker> workers;
    private ArrayList<Item> items;

    public void setAisles(String aisles) {
        this.aisles = aisles;
    }

    public void setSections(String sections) {
        this.sections = sections;
    }

    public void setShelves(String shelves) {
        this.shelves = shelves;
    }

    public void setPackagingAreas(String[] packagingAreas) {
        this.packagingAreas = packagingAreas;
    }

    public void setWorkers(ArrayList<Worker> workers) {
        this.workers = workers;
    }

    public void setItems(ArrayList<Item> items) {
        this.items = items;
    }

    public String getAisles() {
        return aisles;
    }

    public String getSections() {
        return sections;
    }

    public String getShelves() {
        return shelves;
    }

    public String[] getPackagingAreas() {
        return packagingAreas;
    }

    public ArrayList<Worker> getWorkers() {
        return workers;
    }

    public ArrayList<Item> getItems() {
        return items;
    }

}

说明默认情况下 spring 启动时启用了几个HttpMessageConverter 其中之一是MappingJacksonHttpMessageConverter ,它将您的 json 转换为 java object。

见此 http 消息转换器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM