我从一开始就从春天迁移到火花,现在我陷入了基本的困境。
当我发出POST请求在正文中发送数据时,我希望将JAVA对象放回控制器中。
春天我曾经做过
@RequestBody User user
它已经被填满了#34;自动..
现在有了火花我有方法:
request.body();
但是这给了我一个像这样的序列化字符串:
id=7&name=Pablo+Mat%C3%ADas&lastname=Gomez&githubUsername=pablomatiasgomez
那么如何获得用户DTO?
当然,User类具有属性
答案 0 :(得分:7)
AFAIK,Spark不提供此功能。当我将它用于一个小型宠物项目时,我写了一些小的实用程序方法来将URL编码的字符串解析为POJO,如下所示:
import com.google.gson.Gson;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.LinkedHashMap;
import java.util.Map;
public class Test {
private static final Gson GSON = new Gson();
public static <T> T convert(String urlencoded, Class<T> type) {
try {
Map<String, Object> map = asMap(urlencoded);
String json = GSON.toJson(map);
return GSON.fromJson(json, type);
}
catch (Exception e) {
e.printStackTrace(); // TODO log
return null;
}
}
public static Map<String, Object> asMap(String urlencoded) throws UnsupportedEncodingException {
return asMap(urlencoded, "UTF-8");
}
@SuppressWarnings("unchecked")
public static Map<String, Object> asMap(String urlencoded, String encoding) throws UnsupportedEncodingException {
Map<String, Object> map = new LinkedHashMap<>();
for (String keyValue : urlencoded.trim().split("&")) {
String[] tokens = keyValue.trim().split("=");
String key = tokens[0];
String value = tokens.length == 1 ? null : URLDecoder.decode(tokens[1], encoding);
String[] keys = key.split("\\.");
Map<String, Object> pointer = map;
for (int i = 0; i < keys.length - 1; i++) {
String currentKey = keys[i];
Map<String, Object> nested = (Map<String, Object>) pointer.get(keys[i]);
if (nested == null) {
nested = new LinkedHashMap<>();
}
pointer.put(currentKey, nested);
pointer = nested;
}
pointer.put(keys[keys.length - 1], value);
}
return map;
}
public static void main(String[] args) {
String payload = "id=7&name=Pablo+Mat%C3%ADas&lastname=Gomez&githubUsername=pablomatiasgomez";
User user = convert(payload, User.class);
System.out.println(user);
}
}
class User {
long id;
String name;
String lastname;
String githubUsername;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", lastname='" + lastname + '\'' +
", githubUsername='" + githubUsername + '\'' +
'}';
}
}
运行此Test
课程将在您的控制台上打印以下内容:
User{id=7, name='Pablo Matías', lastname='Gomez', githubUsername='pablomatiasgomez'}
请注意,当User
中有嵌套结构时,例如Address
由其他几个字段组成时,这也有效。 jus将字段与"."
分开,如下所示:
public class Test {
// ... same code ...
public static void main(String[] args) {
String payload = "id=7&name=Pablo+Mat%C3%ADas&lastname=Gomez&githubUsername=pablomatiasgomez&" +
"address.street=Coolsingel&address.number=42a&address.city=Rotterdam";
User user = convert(payload, User.class);
System.out.println(user);
}
}
class User {
long id;
String name;
String lastname;
String githubUsername;
Address address;
@Override
public String toString() {
return "User{" +
"\n id=" + id +
"\n name='" + name + '\'' +
"\n lastname='" + lastname + '\'' +
"\n githubUsername='" + githubUsername + "'" +
"\n address=" + address + "\n" +
'}';
}
}
class Address {
String street;
String number;
String city;
@Override
public String toString() {
return "Address{" +
"street='" + street + '\'' +
", number='" + number + '\'' +
", city='" + city + '\'' +
'}';
}
}
将打印:
User{ id=7 name='Pablo Matías' lastname='Gomez' githubUsername='pablomatiasgomez' address=Address{street='Coolsingel', number='42a', city='Rotterdam'} }
如果有效负载包含一个列表,比如说User
,你可以这样做:
public class Test {
private static final Gson GSON = new Gson();
public static <T> T convert(String urlencoded, Type type) {
try {
Map<String, Object> map = asMap(urlencoded);
String json = GSON.toJson(containsList(map) ? map.values() : map);
return GSON.fromJson(json, type);
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static boolean containsList(Map<String, Object> map) {
return !map.isEmpty() && new ArrayList<>(map.keySet()).get(0).contains("[");
}
public static Map<String, Object> asMap(String urlencoded) throws UnsupportedEncodingException {
return asMap(urlencoded, "UTF-8");
}
@SuppressWarnings("unchecked")
public static Map<String, Object> asMap(String urlencoded, String encoding) throws UnsupportedEncodingException {
Map<String, Object> map = new LinkedHashMap<>();
for (String keyValue : urlencoded.trim().split("&")) {
String[] tokens = keyValue.trim().split("=");
String key = tokens[0];
String value = tokens.length == 1 ? null : URLDecoder.decode(tokens[1], encoding);
String[] keys = key.split("\\.");
Map<String, Object> pointer = map;
for (int i = 0; i < keys.length - 1; i++) {
String currentKey = keys[i];
Map<String, Object> nested = (Map<String, Object>) pointer.get(keys[i]);
if (nested == null) {
nested = new LinkedHashMap<>();
}
pointer.put(currentKey, nested);
pointer = nested;
}
pointer.put(keys[keys.length - 1], value);
}
return map;
}
public static void main(String[] args) throws Exception {
String payload = "id=7&name=Pablo Mat%C3%ADas";
User user = convert(payload, User.class);
System.out.println("single user -> " + user);
payload = "users[0].id=7&users[0].name=Pablo Mat%C3%ADas&users[1].id=42&users[1].name=Bart";
List<User> users = convert(payload, new TypeToken<List<User>>(){}.getType());
System.out.println("list of users -> : " + users);
}
}
class User {
long id;
String name;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
将打印:
single user -> User{id=7, name='Pablo Matías'} list of users -> : [User{id=7, name='Pablo Matías'}, User{id=42, name='Bart'}]
答案 1 :(得分:3)
我发现了一种不涉及URL编码的简单方法。
在客户端上,将您的javascript对象转换为JSON字符串并使用它设置查询参数(yourObject):
var obj = null;
obj = {
yourObject: JSON.stringify(currentObject)
};
$.ajax({
type: "GET",
url: "saveAnObject",
data: obj,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
console.log('saveAnObject result: ' + data + ".");
},
error: function() {
},
cache: false
});
然后在Spark:
get("/saveAnObject", (req, res) - > {
String yourObjectStr = "" + req.queryParams("yourObject");
// Convert the JSON string to a POJO obj
Gson gson = new GsonBuilder().create();
YourObject pojoObj = gson.fromJson(yourObjectStr , YourObject.class);
// do something with your pojoObj object.