在使用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的转换器(注解方式)的更多相关文章

  1. SpringMVC 控制器写多个方法(非注解方式)

    Controller类有两种方法 1,implements Controller(实现Controller接口) 2,extends MultiActionController(继承 MultiAct ...

  2. Spring-MVC配置Gson做为Message Converter解析Json

    Spring-MVC配置Gson做为Message Converter解析Json 在学习Spring的时候看到可以使用@RequestBody 和@ResponseBody注解来是的Spring自动 ...

  3. SpringMVC源码剖析5:消息转换器HttpMessageConverter与@ResponseBody注解

    转自 SpringMVC关于json.xml自动转换的原理研究[附带源码分析] 本系列文章首发于我的个人博客:https://h2pl.github.io/ 欢迎阅览我的CSDN专栏:Spring源码 ...

  4. springboot+springmvc拦截器做登录拦截

    springboot+springmvc拦截器做登录拦截 LoginInterceptor 实现 HandlerInterceptor 接口,自定义拦截器处理方法 LoginConfiguration ...

  5. [转]SpringMVC使用@ResponseBody时返回json的日期格式、@DatetimeFormat使用注意

    一.SpringMVC使用@ResponseBody时返回json的日期格式 前提了解: @ResponseBody 返回json字符串的核心类是org.springframework.http.co ...

  6. springmvc整合fastjson

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  7. SpringMVC使用@ResponseBody时返回json的日期格式、@DatetimeFormat使用注意

    一.SpringMVC使用@ResponseBody时返回json的日期格式 前提了解: @ResponseBody 返回json字符串的核心类是org.springframework.http.co ...

  8. Springmvc 的post请求的json格式参数

    背景: 这两天在项目中遇到了一个问题.我的环境是springmvc4.1.9,写了几个可以用ajax请求的接口(ajax.jsonp 调用正常).突然一时兴起就用 HTTP 请求的工具(比如火狐浏览器 ...

  9. springmvc 请求和响应的json和Object的转换

    就是两个注解的使用@RequestBody和@ResponseBody注解的使用,然后springmvc解析进行转换然后注入 例子: @RequestMapping("/...") ...

随机推荐

  1. <Matlab-3:追赶法(Doolittle分解)工具箱

    function x=chase (d,e,f,b) % --------------------------------------------------------------- %the me ...

  2. CF-787D-线段树建图+最短路

    http://codeforces.com/problemset/problem/787/D 题目大意是给出一个有向图,有N个节点,初始节点在S,询问S到所有点最短路.边的读入方式有三种, 1 u v ...

  3. WCF开发实战系列二:使用IIS发布WCF服务 转

    转 http://www.cnblogs.com/poissonnotes/archive/2010/08/28/1811141.html 上一篇中,我们创建了一个简单的WCF服务,在测试的时候,我们 ...

  4. 遇到后缀名为whl的库的安装方法

    直接把whl文件改成zip文件,解压到site-packages里面,其中site-packages文件夹位于例如我的位置是e:/python34/lib/sit-packages即可,然后就可以用i ...

  5. excel导入 导出

    PHP页面 //设置header header("content-Type:text/html;charset=utf-8"); //设置文件大小的限制 ini_set(" ...

  6. JS 设置盒子div 跳转

    方式一 window.location.href=”url”; 在当前窗口跳转 方式二 window.open(‘url’) 在新窗口跳转 window.open(‘url’,’_self’) 在当前 ...

  7. 【九校联考-24凉心模拟】锻造(forging)

    题目背景 勇者虽然武力值很高,但在经历了多次战斗后,发现怪物越来越难打, 于是开始思考是不是自己平时锻炼没到位,于是苦练一个月后发现……自 己连一个史莱姆都打不过了. 勇者的精灵路由器告诉勇者其实是他 ...

  8. TLS与SSL之间关系——SSL已经被IEFT组织废弃,你可以简单认为TLS是SSL的加强版

    TLS与SSL之间关系 原文地址:SSL vs. TLS - What's the Difference? from:https://juejin.im/post/5b213a0ae51d4506d4 ...

  9. JavaScript中 null 的 typeof是object

    JavaScript中  null 的 typeof是object

  10. Apache支持TRACE请求漏洞处理方案

    trace和get一样是http的一种请求方法,该方法的作用是回显收到的客户端请求,一般用于测试服务器运行状态是否正常. 该方法结合浏览器漏洞可能造成跨站脚本攻击.修复方法如下: 编缉/etc/htt ...