网上大部分是spring3.0的传JSON对象,spring3自带了支持JSON对象。
关于spring2.5,也很简单。导入两个jackson的jar包,然后手动传json对象
jackson-core-asl-1.9.11.jar
jackson-mapper-asl-1.9.11.jar
原来controller里面传ModelAndView:
public ModelAndView test(HttpServletRequest req, HttpServletResponse res)
throws Exception {
return new ModelAndView(view, model);
}
改成传json对象
public ModelAndView test(HttpServletRequest req, HttpServletResponse res)
throws Exception {
ObjectMapper mapper = new ObjectMapper();
res.getOutputStream().print(mapper.writeValueAsString(dinnerList));
res.getOutputStream().flush();
res.getOutputStream().close();
// res.getOutputStream().write(new String("No parameter found.").getBytes());
return null;
}

本文介绍如何在Spring 2.5中通过引入Jackson库实现JSON对象的传输。通过示例展示了如何将对象转换为JSON字符串并发送到客户端。
325

被折叠的 条评论
为什么被折叠?



