spring mvc如何优雅的使用fastjson
2024-10-15 07:40:26
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
最新文章
- UWP学习记录8-设计和UI之控件和模式5
- Nova PhoneGap框架 第八章 滚动条
- Java开发面试总结
- HTTP 头字段总结
- K650D安装黑苹果
- position中多次用到了relative和absolute,能不能具体介绍一下这两者的区别?
- 学习练习 使用Servlet实现用户注册功能
- Unity给力插件之ShaderForge(一)
- 错误ERROR datanode.DataNode (DataXceiver.java:run(278)) - hadoop07:50010DataXceiver error processing unknown operation src:127.0.0.136479 dst:127.0.0.150010
- java_Timer_schedule jdk自带定时器
- 12c windows auto installl
- The first to Python
- ajax删除数据(不跳转页面)
- UPS不间断电源网络功能介绍
- Java的Random总结
- Tomcat8-windows不能在本地计算机启动tomcat,有更多的信息,查阅系统事件日志。
- 【Luogu P2664】树上游戏
- [js] - js中类(伪)数组装正规数组
- shell中调用R语言并传入参数的两种步骤
- R语言编程艺术(1)快速入门
热门文章
- [ActionScript 3.0] 实现放大镜效果的简单方法
- java多线程那点事
- 可持久化数据结构QwQ(持续更新中)
- P5038 [SCOI2012]奇怪的游戏 二分+网络流
- JQ的ready()方法与window.onload()的区别与联系
- 7.pytest中的 plugin
- jupyter notebook不小心点了退出,怎么找到密码
- 论文阅读 | CenterNet:Object Detection with Keypoint Triplets
- iis+php(FastCGI)
- Python Requests IP直连