手动渲染ModelAndView?

时间:2011-06-30 08:04:43

标签: ajax json spring-mvc

我需要手动在我的控制器中渲染ModelAndView,以便将它放在JSON对象中。如果我将整个ModelAndView对象传递给JSON,我得到“没有找到类javassistlazyinitializer的序列化程序”异常,因为jackson无法与LAZY对象一起正常工作。 谢谢

1 个答案:

答案 0 :(得分:0)

public class JSONView implements View {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(JSONView.class);

    private String contentType = "application/json";

    public void render(Map map, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    if(logger.isDebugEnabled()) {
        logger.debug("render(Map, HttpServletRequest, HttpServletResponse) - start");
    }

    JSONObject jsonObject = new JSONObject(map);
    PrintWriter writer = response.getWriter();
    writer.write(jsonObject.toString());

    if(logger.isDebugEnabled()) {
        logger.debug("render(Map, HttpServletRequest, HttpServletResponse) - end");
    }
    }

    public String getContentType() {
    return contentType;
    }

}

ModelAndView returnModelAndView = new ModelAndView(new JSONView(), model);