【问题标题】:parameters and inheritance in spring-mvc via jsonspring-mvc中的参数和继承通过json
【发布时间】:2011-04-17 00:33:48
【问题描述】:

我需要一个建议。我正在尝试将对象列表作为参数传递给使用 spring-mvc 3 编写的方法控制器,所有这些都使用 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');
            });

服务器端

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
}

控制器(这里的问题)

@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"));
    }

所以,当调用方法控制器时,Gson 创建了一个 List 适配器,所有元素都属于 WidgetAdapter 类,而不是 TextWidgetAdapter 或 YoutubeWidget。有没有办法做到这一点??我的意思是将参数作为类的元素后代列表传递并由 Gson 正确转换?

谢谢,我希望清楚。英语不是我的母语。

pd:我正在以一种好的方式或更好的方式来做这一切。

【问题讨论】:

    标签: java jquery json spring-mvc


    【解决方案1】:

    好的!我告诉你我是如何解决这个问题的。请告诉我你的意见。 我创建了一个自定义反序列化器(在这里我创建了具体实例)。代码如下:

    @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);
        }
    
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-23
      • 2011-07-13
      • 1970-01-01
      • 2014-12-25
      • 2020-06-08
      • 1970-01-01
      • 2016-05-02
      • 2018-05-25
      相关资源
      最近更新 更多