我需要建议。我试图将对象列表作为参数传递给用 spring-mvc 3 编写的方法 Controller ,所有这些都使用 ajax。这些对象是小部件类的后代。我先给你看代码
ajax 调用:
function buildWidget(obj){
var result = null;
switch(obj.className){
case 'text':
result = {
x:obj.control.x
,y:obj.control.y
,height: obj.control.height
,width: obj.control.width
,text: obj.text
};
break;
case 'youtube':
result = {
x:obj.control.x
,y:obj.control.y
,height: obj.control.height
,width: obj.control.width
,videos: obj.videos
};
break;
}
return result;
}
var all = new Array();
for(i=0;i<figures.size;i++)
all.push(buildWidget(figures.data[i].widget));
jQuery.getJSON("pages/save.html", { widgetsList: jQuery.toJSON(all) }, function(myresult) {
alert('ok');
});
服务器端
类(class)
public class WidgetAdapter {
private int x;
private int y;
private int width;
private int height;
// getters and setters
}
public class TextWidgetAdapter extends WidgetAdapter {
private String text;
// getters and setters
}
public class YoutubeWidget extends WidgetAdapter{
private String videos;
// getters and setter
}
Controller (这里的问题)
@RequestMapping(value = "/pages/save",method=RequestMethod.GET)
public @ResponseBody String save(@RequestParam String widgetsList){
Type listType = new TypeToken<List<WidgetAdapter>>() {}.getType();
List<WidgetAdapter> adapters = new Gson().fromJson( widgetsList, listType);
for(WidgetAdapter a :adapters){
System.out.println(a.getX());
}
return new Gson().toJson(new ActionResult("msg"));
}
因此,当 Controller 调用方法 Gson 创建一个 List 适配器时,所有元素都是 WidgetAdapter 类,而不是 TextWidgetAdapter 或 YoutubeWidget。有办法实现吗?我的意思是传递参数作为类的元素后代列表并由 Gson 正确转换?
谢谢,我希望能说清楚。英语不是我的母语。
pd:我正在以一种好的方式做这一切,或者存在更好的方式。
最佳答案
好的!我告诉你我是如何解决这个问题的。请告诉我你的意见。 我创建了一个自定义反序列化器(在这里我创建了具体实例)。这是代码:
@RequestMapping(value = "/pages/save",method=RequestMethod.GET)
public @ResponseBody String save(@RequestParam String widgetsList){
GsonBuilder gson = new GsonBuilder();
// register the custom deserializer for my WidgetAdapterClass
gson.registerTypeAdapter(WidgetAdapter.class, new WidgetDeserialization());
Type listType = new TypeToken<List<WidgetAdapter>>() {}.getType();
// create gson an deserialize object
List<WidgetAdapter> adapters = gson.create().fromJson( widgetsList, listType);
// just for testing proposes
for(WidgetAdapter a :adapters){
System.out.println(a.getWidth());
}
return new Gson().toJson(new ActionResult("Ok"));
}
具体的解串器
public class WidgetDeserialization implements JsonDeserializer<WidgetAdapter> {
/***
* factory method for custom WidgetAdapter
* @param json
* @return WidgetAdapter
*/
private WidgetAdapter getWidget(JsonObject json){
WidgetAdapter adapter = null;
//obtain widget class. Util for instanciate concrete class
String className = json.get("className").getAsString();
// create concrete classes and set concrete atributes
if("text".equals(className)){
adapter = new TextWidgetAdapter(json.get("text").getAsString());
}else if("youtube".equals(className)){
adapter = new YoutubeWidgetAdapter(json.get("videos").getAsString());
}
// if adapter is created common atributes are set
if(adapter!=null){
adapter.setHeight(json.get("height").getAsInt());
adapter.setWidth(json.get("width").getAsInt());
adapter.setX(json.get("x").getAsInt());
adapter.setY(json.get("y").getAsInt());
}
return adapter;
}
@Override
public WidgetAdapter deserialize(JsonElement element, Type arg1,
JsonDeserializationContext arg2) throws JsonParseException {
JsonObject obj = element.getAsJsonObject();
return getWidget(obj);
}
}
希望对你有帮助
关于java - spring-mvc中通过json的参数和继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5690633/