首先这个项目得是maven项目,不是maven项目的自己引包,我就不多说了.

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.24</version>
</dependency>

导完包就可以用了,不过切记切记

这个扫描注解的路径设置,千万千万不能设置成com

下面我出现问题的时候就是因为这里设置成com了,导致fastjson的fastJsonpResponseBodyAdvice这个方法也被开启了,所以要配置下面说的jsonp的问题

各位朋友千万注意...如果不需要jsonp,这里千万不能懒省事...

-----------------------------------以下内容已经过时,没必要再看--------------------------------------------

由于我个人喜欢在创建项目时,maven里使用最新的包,

所以,在去年12月份使用spring-webmvc 4.3.5版本和fastjson1.2.23时,

就出现了项目无法启动的问题,当时真的困扰了我很久,还专门在fastjson的github上提问https://github.com/alibaba/fastjson/issues/996

由于没有那么多时间等,于是一遍与作者沟通,一边换回了fastjson1.2.17,放弃了最新的特性JSONArray.toJavaList(.class)的方法

终于,今天再次开启了新的项目,再次遇到了这个问题,这次终于解决了.

maven包:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.24</version>
</dependency>

DispatcherServlet-context.xml

<mvc:annotation-driven>
<!--配置@ResponseBody由fastjson解析-->
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="defaultCharset" value="UTF-8"/>
</bean>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4"/>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:default-servlet-handler />
<bean id="fastJsonpResponseBodyAdvice" class="com.alibaba.fastjson.support.spring.FastJsonpResponseBodyAdvice">
<constructor-arg>
<list>
<value>callback</value>
<value>jsonp</value>
</list>
</constructor-arg>
</bean>

这样配置就可以了.

前一个配置项可以不要,作用是遇到@ResponseBody注解的方法时,帮你转成json形式,比如下面的

@RequestMapping("/test")
public ResponseEntity<Object> test(){
List<Object> objectList=new ArrayList<Object>();
return new ResponseEntity<Object>(objectList,new HttpHeaders(), HttpStatus.OK);
}

这里只是示意,到时候你往return的ResponseEntity里第一个参数放什么东西,不管是单个对象还是数组,都能转成json,如果是放JsonObject或者是JsonArray,就不会再画蛇添足的加引号了

而下一个配置必须要要,因为之前项目运行不成功的时候就是因为缺少了fastJsonpResponseBodyAdvice这个方法,不知为何不设置成默认关闭,一定要配置

最新文章

  1. Java 基础知识 问答
  2. Ubuntu 13.10 Broadcom BCM4313问题
  3. python FTP上传和下载文件
  4. Java之文件的随机访问和读写RandomAccessFile
  5. 网站SEO,HTTP请求的关键数字----6
  6. mysql中的范式与范式——读&lt;&lt;高性能mysql&gt;&gt;笔记一
  7. POJ 2832 How Many Pairs?
  8. 《JAVA程序设计》_第八周学习总结
  9. 【codeforces 765F】Souvenirs
  10. LeetCode 547 朋友圈
  11. 学习Spring中遇到关于BeanFactory及测试类的问题
  12. February 12th, 2018 Week 7th Monday
  13. 洛谷P3168 任务查询系统
  14. 记laravel5.5项目php-fpm迁移到swoole4.2.9
  15. http-从域名到页面
  16. 使用 Hexo + github 搭建个人博客
  17. 开始学习jQuery
  18. Oracle ocp 12c-071最新考试题库及答案-1
  19. IDEA里运行代码时出现Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger的解决办法(图文详解)
  20. [ python ] 类的组合

热门文章

  1. activemq消息队列的使用及应用docker部署常见问题及注意事项
  2. golang学习笔记6 beego项目路由设置
  3. 什么是 shell
  4. [转载]oracle的加密和解密
  5. jt项目日志查询流程
  6. Python 一些有趣的技巧,包括协程例
  7. 基于rsync的lsyncd自动同步配置
  8. Java Thread.yield详解
  9. python简说(十五)MD5加密
  10. poj 2096 Collecting Bugs - 概率与期望 - 动态规划