[英]How to convert a POST API call with a JSON body to a java class in Springboot?
i'm passing a POST request with raw JSON body which holds the configuration details, i want to save it as a java class so i can distribute the data using getters to relevant classes that need the config details specified for them.This is the body of my request我正在使用包含配置详细信息的原始 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
}
]
}
and i want to the details of that body into my config.java class, this is the config.java class我想将该主体的详细信息放入我的配置中
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;
}
}
And i have modelled the worker and item classes with the same variables as in the json configuration file, is there a direct way to convert this JSON file to a class?我已经使用与 json 配置文件中相同的变量对工人和物品类进行了建模,有没有直接的方法可以将此 JSON 文件转换为 class? if not what other methods can i try?如果不是我可以尝试什么其他方法? Thanks in advance!提前致谢!
edit- This is the endpoint i have created, using @Rest Controller编辑-这是我创建的端点,使用@Rest Controller
@RequestMapping(value ="/config", method = RequestMethod.POST)
public void configure() {
//i want to do the conversion here
}
@RequestMapping(value ="/config", method = RequestMethod.POST)
public void configure(@RequestBody Config config) {
//your json is converted to config java object
}
Config
class更新您的Config
classpublic 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;
}
}
Explanation By default spring boot comes with several HttpMessageConverter
s enabled.说明默认情况下 spring 启动时启用了几个HttpMessageConverter
。 One of them is MappingJacksonHttpMessageConverter
which converts your json to java object.其中之一是MappingJacksonHttpMessageConverter
,它将您的 json 转换为 java object。
See this http message converter见此 http 消息转换器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.