1. 在spring mvc中配置fastjson

<!-- 设置配置方案 -->
<mvc:annotation-driven>
<!-- 设置不使用默认的消息转换器 -->
<mvc:message-converters register-defaults="false">
<!-- 配置Spring的转换器, 字符编码 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" index="0"/>
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
<!--配置fastjson中实现HttpMessageConverter接口的转换器-->
<bean id="fastJsonHttpMessageConverter"
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<!-- 加入支持的媒体类型:返回contentType -->
<property name="supportedMediaTypes">
<list>
<!-- 这里顺序不能反,一定先写text/html,不然ie下会出现下载提示 -->
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<!--枚举类型,对于返回List集合中引用同一个对象,忽略引用检测【注意不要出现循环引用现象】-->
<property name="features">
<list>
<value>DisableCircularReferenceDetect</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

2. 使用方法

    // 文章details
@RequestMapping(value = "/detail", method = RequestMethod.GET)
public String detail(Long id, Model model, HttpServletRequest request){
Article article = articleService.getArticleById(id, request); Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
if(teacher==null){
return "login.jsp";
}
// role : 1-teacher
ArticleZan zan = articleService.getZan(article.getArticle_id(), 1, teacher.getTe_id());
model.addAttribute("article", article);
model.addAttribute("zan",zan);
return "article/detail.jsp";
}

  {"id":1}  ->可以完成参数疯转

  

  对于普通的参数,fastjson可以完成参数封装和类型转换。但是对于JSON数据中有数组就无能为力了:例如:

  解决办法:

   @CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping(value = "/getArticles")
@ResponseBody
public Object getArticles(Long[] id, @RequestBody JSONObject[] obj, HttpServletRequest request){
Set<Long> ids = new HashSet<>();
for (JSONObject o :
obj) {
ids.add(o.getLong("id"));
}
List<Article> articles = articleService.getArticles(ids, request);
return articles;
}

end

spring mvc如何优雅的使用fastjson的更多相关文章

  1. spring mvc + freemarker优雅的实现邮件定时发送

    1. spring mvc工程中引入相关freemarker\mail的包 如:pom.xml中加入类似 <dependency> <groupId>javax.mail< ...

  2. Spring MVC整合fastjson、EasyUI乱码问题

    一.框架版本 Spring MVC:spring-webmvc-4.0.0.RELEASE fastjson:fastjson-1.2.45 EasyUI:1.5 二.乱码现象 Controller调 ...

  3. 我是如何进行Spring MVC文档翻译项目的环境搭建、项目管理及自动化构建工作的

    感兴趣的同学可以关注这个翻译项目 . 我的博客原文 和 我的Github 前段时间翻译的Spring MVC官方文档完成了第一稿,相关的文章和仓库可以点击以下链接.这篇文章,主要是总结一下这个翻译项目 ...

  4. Spring MVC中文文档翻译发布

    前后经过九个月,我翻译的Spring MVC官方4.2.4版本中文文档可以发布第一个较为完整的版本了.译文上尽量做到准确并且符合中文习惯,让人能读懂,能理解.现全文发布如下,也希望它能够给出其价值,并 ...

  5. Spring MVC注解的一些案列

    1.  spring MVC-annotation(注解)的配置文件ApplicationContext.xml <?xml version="1.0" encoding=& ...

  6. Maven+Spring MVC Spring Mybatis配置

    环境: Eclipse Neon JDK1.8.0 Tomcat8.0 先决条件: Eclipse先用maven向导创建web工程.参见本站之前随笔. 本机安装完成mysql5:新建用户xuxy03设 ...

  7. Spring MVC静态资源处理

    优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往 ...

  8. 【WEB】初探Spring MVC框架

    Spring MVC框架算是当下比较流行的Java开源框架.但实话实说,做了几年WEB项目,完全没有SpringMVC实战经验,乃至在某些交流场合下被同行严重鄙视“奥特曼”了.“心塞”的同时,只好默默 ...

  9. Java框架搭建-Maven、Mybatis、Spring MVC整合搭建

    1. 下载eclipse 到网站下载 http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/marsr 选择 ...

随机推荐

  1. 2018OCP最新题库052新加考题及答案整理-27

    27.Examine these facts about a database: 1. USERS is the database default tablespace. 2. USER1, USER ...

  2. js计算机样式window.getComputedStyle(ele,null)2

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. windows下安装ubuntu15.04

    本文主要介绍windows下安装ubuntu15.04,对与其他的版本也是适用的.现在要讲的是一种最简单ubuntu的安装方式. 1软件下载 1.磁盘分区工具DiskGenius 2.启动项修改工具E ...

  4. docker安装mysql57

    提升应用交付效率 1. 支持服务发现,避免服务重启迁移 IP 变更带来影响:2. 支持微服务化,降低代码维护及重构复杂度,适应快速变化的业务需求. 快速响应业务变化 1. 灵活水平扩展,应对业务量的骤 ...

  5. php-elasticsearch scroll分页详解

    背景 ps:首先我们在一个索引里面写入一万条以上的数据.作为数据源 现在我想看到第一万零一条数据,首先第一想法是,from 10000 size 1 ,这样做会包下面错误.显然是不成立的.此时便会用到 ...

  6. TX2之多线程读取视频及深度学习推理

    背景 一般在TX2上部署深度学习模型时,都是读取摄像头视频或传入视频文件进行推理,从视频中抽取帧进行目标检测等任务.对于大点的模型,推理的速度是赶不上摄像头或视频的帧率的,如果我们使用单线程进行处理, ...

  7. 清除bean中所有非基本数据类型的属性值

    利用beanutils清除javabean中所有非基本数据类型的属性值: import com.google.gson.Gson; import lombok.Data; import org.apa ...

  8. Angular material mat-icon 资源参考_Toggle

    ul,li>ol { margin-bottom: 0 } dt { font-weight: 700 } dd { margin: 0 1.5em 1.5em } img { height: ...

  9. [转] 公共DNS,114.114.114.114和8.8.8.8

    [From] https://zhidao.baidu.com/question/304558434158495364.html 一.两者的联系 114.114.114.114和8.8.8.8,这两个 ...

  10. orange1