以前项目中对于JSON数据的返回,一直都是如下代码:
@RequestMapping("inittree.json")
@ResponseBody
public List<ZtreeNode> initTree() {
System.out.println("加载顶层树");
List<ZtreeNode> list = new ArrayList<ZtreeNode>();
for (int i = 1; i <= 10; i++) {
list.add(new ZtreeNode(i, 0, "父节点-" + i));
}
return list;
}
但是以前框架是项目经理搭的,现在自己操练起来居然有问题!
资料都翻烂了,大部分人给出的答案都是需要jackson-core-asl-1.9.12.jar,jackson-mapper-asl-1.9.12.jar,亲测,不加也可以。
最后找到一篇文章写得在spring配置文件中配置一些东西就可以搞定,我测试过,确实可以,但是不知道为什么以前的项目都没这样配置。
在Spring的配置文件中加入如下代码:
<context:annotation-config />
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
<bean id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="favorParameter" value="false" />
<property name="ignoreAcceptHeader" value="false" />
<property name="mediaTypes">
<value>
atom=application/atom+xml
html=text/html
json=application/json
*=*/*
</value>
</property>
</bean>
Spring的schema要使用3.2.xsd,3.0的不行
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
已经完全搞不懂为什么了,有时间再去研究。
方案来自博客:http://blog.sina.com.cn/s/blog_a5f093b40101eo2h.html