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的版 ...
随机推荐
- Chapter 3 Introduction to Objects and Input/Output
与声明一个primitive variable不同,声明一个对象的时候,并不创建用来存储一个对象的内存空间,而是创建了一个存储该对象所在内存空间的地址. 在java里,new是一个操作符,它让系统分配 ...
- TestNG 中DataProvider 的用法
使用DataProvider提供数据有两种形式: 第一种:一种是在测试代码和测试数据放在同一个类中: 第二种:把所有的数据提供都单独写在一个类里面,当测试数据比较多时,这种方法利于维护. DataPr ...
- Servlet 参数
1.应用参数,在web.xml配置,所有Servlet共用 <context-param> <param-name>driver</param-name> < ...
- 变量类型-List
教程:一:列表的创建 List(列表) 是 Python 中使用最频繁的数据类型.列表中元素的类型可以不相同,数字,字符串甚至可以包含列表(所谓嵌套) (1)List写在方括号之间,元素用逗号隔 ...
- CDI feature
CDI Java EE的上下文和依赖注入(Contexts and Dependency Injection for Java EE,CDI),CDI是即将完成的Java EE 6平台的关键组成部分, ...
- python遍历文件
#!/usr/local/bin/python # -*- coding: UTF-8 -*- #coding:gbk import re import os rootdir = 'src' def ...
- Caused by: java.lang.ClassNotFoundException: org.springframework.integration.handler.support.HandlerMethodArgumentResolversHolder
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring ...
- k8s-No.1-概述与架构
本章目录 k8s概述 k8s系统架构 k8s工作流程图 一 概述 k8s是什么 k8s是谷歌公司基于内部容器管理系统borg开源出的一个容器集群管理工具,它是用go语言开发,提供了容器的应用部署,规 ...
- IDEA中创建maven项目后解决main文件夹下目录不全的问题
IDEA创建maven-archetype-webapp项目的时候,创建完成后发现在main文件夹下没有java,resource等源文件夹. 解决方法: 1.选择File->Project S ...
- Elasticsearch学习笔记(八)Elasticsearch的乐观锁并发控制
一.基于_version的乐观锁并发控制 语法:PUT /test_index/test_type/id?version=xxx 更新时带上数据 ...