SpringMvc使用FastJson做为json的转换器(注解方式)
在使用XML方式配置项目,使用fastjson做为Json转换器时通常的在XML内添加如下的配置:
<mvc:message-converters register-defaults="true">
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json"/>
<property name="features">
<array>
<value>WriteMapNullValue</value>
<value>WriteDateUseDateFormat</value>
</array>
</property>
</bean>
</mvc:message-converters>
当项目不使用XML配置而使用全注释方式时,实现WebMvcconfigurer接口并重写extendMessageConverters方法并添加fastjson转换器
@Configuration
@EnableWebMvc
@ComponentScan("com.wey.spring.controller")
public class MvcConfig implements WebMvcConfigurer { @Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/classes/views/");
viewResolver.setSuffix(".jsp");
viewResolver.setViewClass(JstlView.class);
return viewResolver;
} /**
* 使用FastJson做为json转换器
*/
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
List<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(new MediaType(MediaType.TEXT_HTML, Charset.forName("UTF-8")));
mediaTypes.add(new MediaType(MediaType.APPLICATION_JSON, Charset.forName("UTF-8")));
mediaTypes.add(new MediaType(MediaType.APPLICATION_XML, Charset.forName("UTF-8"))); converter.setSupportedMediaTypes(mediaTypes); FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); converter.setFastJsonConfig(fastJsonConfig); converters.add(converter);
}
/**
* 静态资源
*/
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:static/");
}
}
SpringMvc使用FastJson做为json的转换器(注解方式)的更多相关文章
- SpringMVC 控制器写多个方法(非注解方式)
Controller类有两种方法 1,implements Controller(实现Controller接口) 2,extends MultiActionController(继承 MultiAct ...
- Spring-MVC配置Gson做为Message Converter解析Json
Spring-MVC配置Gson做为Message Converter解析Json 在学习Spring的时候看到可以使用@RequestBody 和@ResponseBody注解来是的Spring自动 ...
- SpringMVC源码剖析5:消息转换器HttpMessageConverter与@ResponseBody注解
转自 SpringMVC关于json.xml自动转换的原理研究[附带源码分析] 本系列文章首发于我的个人博客:https://h2pl.github.io/ 欢迎阅览我的CSDN专栏:Spring源码 ...
- springboot+springmvc拦截器做登录拦截
springboot+springmvc拦截器做登录拦截 LoginInterceptor 实现 HandlerInterceptor 接口,自定义拦截器处理方法 LoginConfiguration ...
- [转]SpringMVC使用@ResponseBody时返回json的日期格式、@DatetimeFormat使用注意
一.SpringMVC使用@ResponseBody时返回json的日期格式 前提了解: @ResponseBody 返回json字符串的核心类是org.springframework.http.co ...
- springmvc整合fastjson
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- SpringMVC使用@ResponseBody时返回json的日期格式、@DatetimeFormat使用注意
一.SpringMVC使用@ResponseBody时返回json的日期格式 前提了解: @ResponseBody 返回json字符串的核心类是org.springframework.http.co ...
- Springmvc 的post请求的json格式参数
背景: 这两天在项目中遇到了一个问题.我的环境是springmvc4.1.9,写了几个可以用ajax请求的接口(ajax.jsonp 调用正常).突然一时兴起就用 HTTP 请求的工具(比如火狐浏览器 ...
- springmvc 请求和响应的json和Object的转换
就是两个注解的使用@RequestBody和@ResponseBody注解的使用,然后springmvc解析进行转换然后注入 例子: @RequestMapping("/...") ...
随机推荐
- 遇到后缀名为whl的库的安装方法
直接把whl文件改成zip文件,解压到site-packages里面,其中site-packages文件夹位于例如我的位置是e:/python34/lib/sit-packages即可,然后就可以用i ...
- Oracle数据库,数字强制显示2位小数
在银行.财务等对数字要求敏感的系统中,数字的显示一般有着严格的要求.今遇到一个需求,如题,要求将数字以两位小数的格式显示,如果没有小数,则强制显示为0.例如:123.4 显示为 123.4012 ...
- Oracle EBS SLA 详解(转)
原文地址: Oracle EBS SLA 详解
- 在MongoDB中执行查询、创建索引
1. MongoDB中数据查询的方法 (1)find函数的使用: (2)条件操作符: (3)distinct找出给定键所有不同的值: (4)group分组: (5)游标: (6)存储过程. 文档查找 ...
- mybatis与spring的整合(代码实现)
mybatis与spring的整合(代码实现) 需要jar包: mybatis核心包:依赖包:log4j包:spring croe;beans;tx;aop;aspects;context;expre ...
- 【转】Entity Framework Extended Library (EF扩展类库,支持批量更新、删除、合并多个查询等)
E文好的可以直接看https://github.com/loresoft/EntityFramework.Extended 也可以在nuget上直接安装这个包,它的说明有点过时了,最新版本已经改用对I ...
- OWASP TOP 10 2017中文译文
说明:owasp top 10其实有中文官方版本:本文是按着英文版进行翻译而成. 官方中文版:http://www.owasp.org.cn/owasp-project/OWASPTop102017v ...
- was控制台误禁用后的恢复启用办法
websphere是可以配置禁用控制台的,下面以was6.1.单profile.https控制台为例介绍在(误)禁用控制台后如何恢复启用控制台. 1. 禁用控制台 WCInboundAdmin--控制 ...
- OpenSSH隐藏版本号教程
1.查看当前OpenSSH版本号 sshd -v #sshd并没有-v选项,这并不是重点重点是这样能曝出版本号 2.查看sshd位置 which sshd 3.备份sshd cp /usr/sbin/ ...
- react router @4 和 vue路由 详解(七)react路由守卫
完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 12.react路由守卫? a.在之前的版本中,React Router 也提供了类似的 ...