java 将 RequestBody json 转换为对象 - Spring Boot
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43109818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting a RequestBody json to an Object - Spring Boot
提问by Ajeesh
I am a begineer in java development but has previous experience on programming languages like PHP and Python. So little confused on how to proceed on spring boot with the development.
我是 Java 开发的初学者,但以前有过 PHP 和 Python 等编程语言的经验。对如何在 Spring Boot 上进行开发很少感到困惑。
I am developing a rest API which has the following request
我正在开发一个具有以下请求的休息 API
{
"key":"value",
"key1":"value1",
"platform_settings":[
{"key":"value"}
]
}
What I did
我做了什么
I created a RestController which accepts the http request and created a function for the resource
我创建了一个接受 http 请求的 RestController 并为资源创建了一个函数
public Share share(@RequestBody final Share share) {
LOGGER.debug("This is the request", share);
return share; //
}
Question 1 : If it was any other programming language like PHP or Python, there will be helper function which will accept the json request and convert it to object which I can easily work on.
问题 1:如果是 PHP 或 Python 等任何其他编程语言,将会有帮助函数接受 json 请求并将其转换为我可以轻松处理的对象。
In python it is as simple as
在python中,它很简单
import json
import requests
response = requests.get(...)
json_data = json.loads(response.text)
//can work on json_data anyway I want.
But in java, I will have to create a POJO class, or have Hymanson/JPA entity as dependency which will map the request to a Class (Which I should predefine with the requests).
但是在 java 中,我将不得不创建一个 POJO 类,或者将 Hymanson/JPA 实体作为依赖项,它将请求映射到一个类(我应该用请求预定义)。
Is there any better way I can do this? For every request I make, I will have to create a Class which the request can be mapped to and I will have to define the class
有没有更好的方法可以做到这一点?对于我提出的每个请求,我必须创建一个可以将请求映射到的类,并且必须定义该类
Entity
package com.payunow.socialsharemodule.models;
import java.util.Map;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Share {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String key;
private String key1;
private Map<String,String> platform_settings;
public Share(String name, String description,Map<String,String> platform_settings) {
this.key = key;
this.key1 = key1;
this.platform_settings = platform_settings;
}
//for JPA
public Share() {}
public String getKey() {
return key;
}
public String getKey1() {
return key1;
}
public Map<String,String> getPlatform_settings() {
return platform_settings;
}
}
For every request I make, I will have to create a class defining all its variables inside. Is this the only way to do this?
对于我提出的每个请求,我都必须创建一个类来定义其内部的所有变量。这是唯一的方法吗?
回答by pvpkiran
You need to have Hymanson dependecy for coversion of json to java object. But spring provides it by default, so you don't have to add it explicitly.
您需要有 Hymanson 依赖才能将 json 转换为 java 对象。但是 spring 默认提供了它,所以你不必显式添加它。
You don't need a JPA Entity. This is needed only when you want to store the recieved data into database.
Just to recieve the request you don't have to create a separate pojo class. Look at this code
您不需要 JPA 实体。仅当您要将接收到的数据存储到数据库中时才需要这样做。
只是为了接收请求,您不必创建单独的 pojo 类。看看这个代码
@PostMapping("/json")
public JSONObject getGeneric(@RequestBody String stringToParse){
JSONParser parser = new JSONParser();
JSONObject json = null;
try {
json = (JSONObject) parser.parse(stringToParse);
} catch (ParseException e) {
e.printStackTrace();
}
return json;
}
As you can see here it takes a string as a request and converts it into a generic JSONObject. So basically you can pass any json to this endpoint.
正如您在此处看到的,它将字符串作为请求并将其转换为通用 JSONObject。所以基本上你可以将任何 json 传递给这个端点。