Spring Boot默认的JSON解析框架设置
方案一:启动类继承WebMvcConfigurerAdapter,覆盖方法configureMessageConverters
...
@SpringBootApplication
public class UserApplication extends WebMvcConfigurerAdapter{ @Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters); // 初始化转换器
FastJsonHttpMessageConverter fastConvert = new FastJsonHttpMessageConverter();
// 初始化一个转换器配置
FastJsonConfig fastJsonConfig = new FastJsonConfig();
//用于美化格式,可注释掉 fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 将配置设置给转换器并添加到HttpMessageConverter转换器列表中
fastConvert.setFastJsonConfig(fastJsonConfig); converters.add(fastConvert);
} public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
方案二:在启动类中注入 HttpMessageConverters
...
@SpringBootApplication
public class UserApplication {
/**
* 配置FastJson为Spring Boot默认JSON解析框架
* @return HttpMessageConverters
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1.定义一个converters转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3.在converter中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
// 4.将converter赋值给HttpMessageConverter
HttpMessageConverter<?> converter = fastConverter;
// 5.返回HttpMessageConverters对象
return new HttpMessageConverters(converter);
}
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
mark一下
Spring Boot默认的JSON解析框架设置的更多相关文章
- (4)Spring Boot使用别的json解析框架【从零开始学Spring Boot】
此文章已经废弃,请看新版的博客的完美解决方案: 78. Spring Boot完美使用FastJson解析JSON数据[从零开始学Spring Boot] http://412887952-qq-co ...
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
[原创文章,转载请注明出处] 个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析 ...
- Spring boot之返回json数据
1.步骤: 1. 编写实体类Demo 2. 编写getDemo()方法 3. 测试 2.项目构建 编写实体类Demo package com.kfit; /** * 这是一个测试实体类. */ pub ...
- 十七、springboot配置FastJson为Spring Boot默认JSON解析框架
前提 springboot默认自带json解析框架,默认使用jackson,如果使用fastjson,可以按照下列方式配置使用 1.引入fastjson依赖库: maven: <dependen ...
- 4. 使用别的json解析框架【从零开始学Spring Boot】
转载:http://blog.csdn.net/linxingliang/article/details/51585921 此文章已经废弃,请看新版的博客的完美解决方案: 78. Spring Boo ...
- Spring Boot默认日志logback配置解析
前言 今天来介绍下Spring Boot如何配置日志logback,我刚学习的时候,是带着下面几个问题来查资料的,你呢 如何引入日志? 日志输出格式以及输出方式如何配置? 代码中如何使用? 正文 Sp ...
- springboot使用fastJson作为json解析框架
springboot使用fastJson作为json解析框架 springboot默认自带json解析框架,默认使用jackson,如果使用fastjson,可以按照下列方式配置使用 〇.搭建spri ...
- Spring Boot 学习摘要--关于日志框架
date: 2020-01-05 16:20:00 updated: 2020-01-08 15:50:00 Spring Boot 学习摘要--关于日志框架 学习教程来自:B站 尚硅谷 1. 关于日 ...
- spring boot @ResponseBody转换JSON 时 Date 类型处理方法,Jackson和FastJson两种方式,springboot 2.0.9配置fastjson不生效官方解决办法
spring boot @ResponseBody转换JSON 时 Date 类型处理方法 ,这里一共有两种不同解析方式(Jackson和FastJson两种方式,springboot我用的1.x的版 ...
随机推荐
- Android源代码编译过程及指令
编译Android源代码分为两种情况: 1. 完整编译源码: ./mk_aliphone.sh --> 完整编译脚本 --> 6735 输入对应的编号 --> userdebug ...
- In the beginning, Coders create the repos and blogs
---恢复内容开始--- 这是一个新的博客 print(‘hello new world’) 这个博客叫做 brain in a jar, 不知道以后会不会改名. 只是因为偶然想到了普特南的缸中之脑理 ...
- Mysql SQL执行错误:#1136
情况:在插入数据时可能会遇到这种情况: 原因: 插入时的数据个数与表中的字段个数不一致 解决方法: 检查表中的字段数与代码中所插入的数据字段数是否一致 例如:以下为Salary表中结构 虽然ActI ...
- React(二)组件
1.组件概念: 我理解的组件的概念就是复用性,一个组件开发完成后可以重复使用. 2.简单的组件编写 (1)在src中创建一个components的文件夹,里面创建一个header.js的文件 (2)在 ...
- html5 Canvas绘制时钟以及绘制运动的圆
1.绘制时钟 <!-- js代码 --> <script type="text/javascript"> window.onload=function(){ ...
- __x__(5)0905第二天__网页三大组成部分
根据 W3C 标准,将网页主要分成 3 个部分:结构,表现,行为. 结构: HTML 用于描述页面结构. 表现: CSS 用于控制页面中元素的样式. 行为: JavaScript 用于响应用户操作.
- Go语言基础之time包
Go语言基础之time包 时间和日期是我们编程中经常会用到的,本文主要介绍了Go语言内置的time包的基本用法. Go语言中导入包 Go语言中使用import关键字导入包,包的名字使用双引号(”)包裹 ...
- vue菜鸟从业记:公司项目里如何进行前后端接口联调
最近我的朋友王小闰进入一家新的公司,正好公司项目采用的是前后端分离架构,技术栈是王小闰非常熟悉的vue全家桶,后端用的是Java语言. 在前后端开发人员碰面之后,协商确定好了前端需要的数据接口(扯那么 ...
- linux安装杀毒软件
https://www.cnblogs.com/bingo1024/p/9018212.html
- 使用OMS查询Api Management的调用日志
打开Azure portal,找到要操作的Api Management 实例,点击菜单Monitoring/Logs Schema Tab页搜索"diagnostics",选中Lo ...