I'm using playframework, is there any way to convert a java object into Play recognizable pojo parameter list? For example I have two class
我正在使用playframework,有没有办法将java对象转换为Play可识别的pojo参数列表?例如,我有两个班级
public class A{
B b;
String room;
}
public class B{
String name;
}
When I get a A instance a, I can change it into two parameters
当我得到A实例a时,我可以将它改为两个参数
a.room=myroom&a.b.name=myname
Do I have to travel through a json converted from that instance?
我是否必须通过从该实例转换的json?
Thanks a lot.
非常感谢。
1 个解决方案
#1
0
I wrote a Util to convert Object to playframework friendly parameter list with jackson.databind
我用jackson.databind编写了一个Util来将Object转换为playframework友好参数列表
public static List<MutablePair> getPojoParam(Object base, Object obj, boolean firstLevel) {
List<MutablePair> ret = new ArrayList<MutablePair>();
String realBase = (firstLevel) ? (String) base : ("." + base);
if (obj instanceof Collection) {
int count = 0;
for (Object o : (Collection) obj) {
List<MutablePair> subPair = getPojoParam(base, o, false);
for (MutablePair p : subPair) {
p.left = realBase + "[" + (count++) + "]" + p.left;
ret.add(p);
}
}
} else if (obj instanceof Map) {
Map<Object, Object> map = (Map<Object, Object>) obj;
for (Object k : map.keySet()) {
List<MutablePair> subPair = getPojoParam(k, map.get(k), false);
for (MutablePair p : subPair) {
p.left = realBase + p.left;
ret.add(p);
}
}
} else {
ret.add(new MutablePair(realBase, obj));
}
return ret;
}
Usage:
ATest a = new ATest();
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> data = mapper.readValue(new Gson().toJson(a), Map.class);
List<MutablePair> ret = ObjectToPOJO.getPojoParam("source", data, true);
for (MutablePair key : ret) {
System.out.println(key.left + "=" + key.right);
}
I hope it's right.
我希望是对的。
Add dependencies to use MutablePair and jackson.databind
添加依赖项以使用MutablePair和jackson.databind
require:
- com.fasterxml.jackson.core -> jackson-databind 2.1.0
- org.apache.commons -> commons-lang3 3.1
#1
0
I wrote a Util to convert Object to playframework friendly parameter list with jackson.databind
我用jackson.databind编写了一个Util来将Object转换为playframework友好参数列表
public static List<MutablePair> getPojoParam(Object base, Object obj, boolean firstLevel) {
List<MutablePair> ret = new ArrayList<MutablePair>();
String realBase = (firstLevel) ? (String) base : ("." + base);
if (obj instanceof Collection) {
int count = 0;
for (Object o : (Collection) obj) {
List<MutablePair> subPair = getPojoParam(base, o, false);
for (MutablePair p : subPair) {
p.left = realBase + "[" + (count++) + "]" + p.left;
ret.add(p);
}
}
} else if (obj instanceof Map) {
Map<Object, Object> map = (Map<Object, Object>) obj;
for (Object k : map.keySet()) {
List<MutablePair> subPair = getPojoParam(k, map.get(k), false);
for (MutablePair p : subPair) {
p.left = realBase + p.left;
ret.add(p);
}
}
} else {
ret.add(new MutablePair(realBase, obj));
}
return ret;
}
Usage:
ATest a = new ATest();
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> data = mapper.readValue(new Gson().toJson(a), Map.class);
List<MutablePair> ret = ObjectToPOJO.getPojoParam("source", data, true);
for (MutablePair key : ret) {
System.out.println(key.left + "=" + key.right);
}
I hope it's right.
我希望是对的。
Add dependencies to use MutablePair and jackson.databind
添加依赖项以使用MutablePair和jackson.databind
require:
- com.fasterxml.jackson.core -> jackson-databind 2.1.0
- org.apache.commons -> commons-lang3 3.1