标签:convert idt return ring 16px framework jackson spring 控制
json格式:{"name":"spring","year":30}
map格式:{name=spring, year=30}
1、需要的jar包:
jar包下载地址:https://repo1.maven.org/maven2/com/fasterxml/jackson/core/
2、springMVC.xml (经测试无需配置,已自动注入,此步骤可以跳过)
<bean id="messageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <!-- Support JSON --> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </list> </property> </bean> <bean id="exceptionMessageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver"> <property name="messageConverters"> <list> <!-- Support JSON --> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </list> </property> </bean>
3、在需要返回的方法上注解@ResponseBody,可将Map或List返回成json格式(在return之前,System.out.println(map)依然是原格式,但return到的页面得到的是json格式。
控制器中方法配置:
@ResponseBody @RequestMapping(value="/list") public ModelMap List(){ ModelMap result =new ModelMap(); result.put("name", "tianlan"); result.put("age", 30); return result; }
返回数据:
1、需要的jar包:
2、springMVC:
<bean id="messageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <!-- Support JSON --> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> </list> </property> </bean> <bean id="exceptionMessageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver"> <property name="messageConverters"> <list> <!-- Support JSON --> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> </list> </property> </bean>
3、同上。
目前只测试了则两种,估计在4下也可以用3的配置,但3肯定不能用4的
原文地址:http://www.cnblogs.com/liaozs/p/5750913.html
springMVC使用@ResponseBody返回json
标签:convert idt return ring 16px framework jackson spring 控制
原文地址:http://www.cnblogs.com/springwind2006/p/7519985.html