parameters and inheritance in spring-mvc via json
I need an advice. I'm trying to pass a list of objects as parameters to a method controller written with spring-mvc 3, all this with ajax. These objects are decendants of a widget class. i will show you the code first
the ajax call:
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');
});
the server side
the classes
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
}
the controller (the problem here)
@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"));
}
so, when the method controller is called Gson creates a List adapters, all elements are of class WidgetAdapter and not Te开发者_开发知识库xtWidgetAdapter or YoutubeWidget. Is there a way to acheive that?? i mean pass parameters as list of element decendants of a class and be transformed correctly by Gson?
Thanks, i hope be clear. english is not my native language.
pd: i´m doing all of this in a good way or better way exists.
Ok! I tell you how i solved this problem. Please, tell me your opinion. I created a custom deserializer (here i create concretes instances). Here is the code:
@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"));
}
the concrete deserializer
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);
}
}
I hope it helps
精彩评论