手动渲染的ModelAndView

手动渲染的ModelAndView

本文介绍了手动渲染的ModelAndView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要渲染的ModelAndView在我的控制器手动才能把它里面的JSON对象。如果我通过了整个ModelAndView对象为以JSON我得到没有发现串行器类javassistlazyinitializer异常,因为杰克逊不能LAZY对象正常工作。谢谢

解决方案

 公共类JSONView实现查看{
    / **
     *记录仪这一类
     * /
    私有静态最后记录器记录器= Logger.getLogger(JSONView.class);

    私人字符串的contentType =应用/ JSON;

    公共无效渲染(地图,HttpServletRequest的请求,HttpServletResponse的响应)
        抛出异常{
    如果(logger.isDebugEnabled()){
        logger.debug(渲染(地图,HttpServletRequest的,HttpServletResponse的) - 启动);
    }

    的JSONObject的JSONObject =新的JSONObject(图)
    PrintWriter的作家= response.getWriter();
    writer.write(jsonObject.toString());

    如果(logger.isDebugEnabled()){
        logger.debug(渲染(地图,HttpServletRequest的,HttpServletResponse的) - 端);
    }
    }

    公共字符串的getContentType(){
    返回的contentType;
    }

}
 

的ModelAndView returnModelAndView =新的ModelAndView(新JSONView(),型号);

I need to render ModelAndView in my controller manually in order to put it inside JSON object. If I pass the whole ModelAndView object into to JSON I get " no serializer found for class javassistlazyinitializer" exception because jackson can't work properly with LAZY-objects.Thank you

解决方案
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);

这篇关于手动渲染的ModelAndView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:45