spring mvc如何优雅的使用fastjson
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的更多相关文章
- spring mvc + freemarker优雅的实现邮件定时发送
1. spring mvc工程中引入相关freemarker\mail的包 如:pom.xml中加入类似 <dependency> <groupId>javax.mail< ...
- Spring MVC整合fastjson、EasyUI乱码问题
一.框架版本 Spring MVC:spring-webmvc-4.0.0.RELEASE fastjson:fastjson-1.2.45 EasyUI:1.5 二.乱码现象 Controller调 ...
- 我是如何进行Spring MVC文档翻译项目的环境搭建、项目管理及自动化构建工作的
感兴趣的同学可以关注这个翻译项目 . 我的博客原文 和 我的Github 前段时间翻译的Spring MVC官方文档完成了第一稿,相关的文章和仓库可以点击以下链接.这篇文章,主要是总结一下这个翻译项目 ...
- Spring MVC中文文档翻译发布
前后经过九个月,我翻译的Spring MVC官方4.2.4版本中文文档可以发布第一个较为完整的版本了.译文上尽量做到准确并且符合中文习惯,让人能读懂,能理解.现全文发布如下,也希望它能够给出其价值,并 ...
- Spring MVC注解的一些案列
1. spring MVC-annotation(注解)的配置文件ApplicationContext.xml <?xml version="1.0" encoding=& ...
- Maven+Spring MVC Spring Mybatis配置
环境: Eclipse Neon JDK1.8.0 Tomcat8.0 先决条件: Eclipse先用maven向导创建web工程.参见本站之前随笔. 本机安装完成mysql5:新建用户xuxy03设 ...
- Spring MVC静态资源处理
优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往 ...
- 【WEB】初探Spring MVC框架
Spring MVC框架算是当下比较流行的Java开源框架.但实话实说,做了几年WEB项目,完全没有SpringMVC实战经验,乃至在某些交流场合下被同行严重鄙视“奥特曼”了.“心塞”的同时,只好默默 ...
- Java框架搭建-Maven、Mybatis、Spring MVC整合搭建
1. 下载eclipse 到网站下载 http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/marsr 选择 ...
随机推荐
- 小程序:如何在wxml页面中调用JavaScript函数
早上过来遇到一个这样的bug: 在计算百分比的时候没有保留小数点后2位,从而导致一些无法整除的结果显示太长 一开始,我以为这是一个很普通的bug,既然wxml在页面{{}}内支持简单的运算,我想也应该 ...
- [CTSC2010]星际旅行
https://www.luogu.org/problemnew/show/P4189 题解 模拟费用流. 首先有一个非常好的条件,每个点的限制次数都大于等于这个点的度数. 然后我们可以从\(0\)开 ...
- 用最短的payload绕过WAF(入门)
本文作者:jishuzhain <font color=green>想绕过一个WAF,我们可以用最短的payload来做,这里只是基础示例,望各位大佬勿喷,小弟在此谢过.</font ...
- [ActionScript 3.0] 利用InteractivePNG.as类精确选择识别png图片有像素的区域
用法:如果是把png直接导入flash转换成影片剪辑,只需在影片剪辑属性中勾选为ActionScript导出(x),并把基类里的flash.display.MovieClip替换成Interactiv ...
- ArchLinux "error: required key missing from keyring"
downloading required keys... error: key "C847B6AEB0544167" could not be looked up remotely ...
- linux源码中的核心数据结构
寄存器 pt_regs 进程线程 struct task_struct: 进程,或者是线程数据结构,在include/linux/sched.h里面定义的,与硬件体系结构无关 struct threa ...
- python学习笔记1.4
注释不被程序执行的辅助性说明信息- 单行注释:以#开头,其后内容为注释# 这里是单行注释- 多行注释:以'''开头和结尾''' 这是多行注释第一行这是多行注释第二行 ''' 保留字and elif i ...
- C# 文件读写Helper类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- six
团队序号:6组 团队名称:拯救地球小分队 团队项目的码云地址:https://gitee.com/lwj5950/seflash 此次博客撰写人姓名:刘威骏 学号:2017*****7168 团队中的 ...
- python之类与对象(4)
5. 类的继承 继承可谓是重中之重,也是面向对象编程的核心内容之一.子类可以继承父类的属性以及方法.作为一名测试人员,学习了类之后,我们可以很好的把代码与数据分离开来,有了基本的封装思想.接下来我们将 ...