1、第一种方式是spring2时代的产物,也就是每个json视图controller配置一个Jsoniew。
如:<bean id="defaultJsonView" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
或者<bean id="defaultJsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
同样要用jackson的jar包。
2、第二种使用JSON工具将对象序列化成json,常用工具Jackson,fastjson,gson。
利用HttpServletResponse,然后获取response.getOutputStream()或response.getWriter()
直接输出。
示例:
- public class JsonUtil
- {
- private static Gson gson=new Gson();
- /**
- * @MethodName : toJson
- * @Description : 将对象转为JSON串,此方法能够满足大部分需求
- * @param src
- * :将要被转化的对象
- * @return :转化后的JSON串
- */
- public static String toJson(Object src) {
- if (src == null) {
- return gson.toJson(JsonNull.INSTANCE);
- }
- return gson.toJson(src);
- }
- }
3、第三种利用spring mvc3的注解@ResponseBody
例如:
- @ResponseBody
- @RequestMapping("/list")
- public List<String> list(ModelMap modelMap) {
- String hql = "select c from Clothing c ";
- Page<Clothing> page = new Page<Clothing>();
- page.setPageSize(6);
- page = clothingServiceImpl.queryForPageByHql(page, hql);
- return page.getResult();
- }
然后使用spring mvc的默认配置就可以返回json了,不过需要jackson的jar包哦。
注意:当springMVC-servlet.xml中使用<mvc:annotation-driven />时,如果是3.1之前已经默认注入AnnotationMethodHandlerAdapter,3.1之后默认注入RequestMappingHandlerAdapter只需加上上面提及的jar包即可!
如果是手动注入RequestMappingHandlerAdapter 可以这样设置
配置如下:
<div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; width: 936.5333251953125px; overflow: auto; padding-top: 1px; margin: 18px 0px !important; "><div class="bar" style="padding-left: 45px; "><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(108, 226, 108); "><strong>[html]</strong> <a target=_blank href="http://blog.csdn.net/shan9liang/article/details/42181345#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; ">view plain</a><span data-mod="popu_168"> <a target=_blank href="http://blog.csdn.net/shan9liang/article/details/42181345#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; ">copy</a></span><div style="position: absolute; left: 487px; top: 1821px; width: 29px; height: 14px; z-index: 99; "><embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="29" height="14" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=29&height=14" wmode="transparent" /></div><span data-mod="popu_169"> <a target=_blank href="http://blog.csdn.net/shan9liang/article/details/42181345#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; ">print</a></span><a target=_blank href="http://blog.csdn.net/shan9liang/article/details/42181345#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; ">?</a></div></div><ol start="1" class="dp-xml" style="margin: 0px 0px 1px 45px !important; padding: 0px; border: none; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); "><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; "><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; "><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; "><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">bean</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit; ">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; ">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; ">"org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; "><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; "> <span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit; ">p:ignoreDefaultModelOnRedirect</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; ">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; ">"true"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> </span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; "><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; "> <span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; "><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">property</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit; ">name</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; ">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; ">"messageConverters"</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; "><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; "> <span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; "><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">list</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; "><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; "> <span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; "><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">bean</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit; ">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; ">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; ">"org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">/></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; "><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; "> <span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; "></</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">list</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; "><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; "> <span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; "></</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">property</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; "><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; "> <span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; "></</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">bean</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> </span></span></li></ol></div>
添加包
jackson-mapper-asl-*.jar
jackson-core-asl-*.jar