SpringMVC+fastjson项目配置

时间:2023-03-09 04:58:09
SpringMVC+fastjson项目配置

首先这个项目得是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>

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

SpringMVC+fastjson项目配置

这个扫描注解的路径设置,千万千万不能设置成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这个方法,不知为何不设置成默认关闭,一定要配置