SpringBoot-01SpringBoot配置FastJson
来源:互联网 发布:mysql数据库同步 日志 编辑:程序博客网 时间:2024/05/01 03:22
一. SpringBoot自带JSON解析工具
通过观察SpringBoot自动引入的jar包可以发现, SpringBoot其实自身已经携带JSON解析工具:Jackson. 如果不想使用Jackson而使用FastJson的话, 需要在SpringBoot中配置FastJson.
二. 步骤
1. 在pom.xml中引入FastJson依赖库
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency>
2. 配置FastJson(两种方式)
方式一:
① SpringBoot启动类继承WebMvcConfigurerAdapter
② 重写configureMessageConverters()方法
package online.bendou.springboot.fastjsondemo;import java.util.List;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.http.MediaType;import org.springframework.http.converter.HttpMessageConverter;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import com.alibaba.fastjson.serializer.SerializerFeature;import com.alibaba.fastjson.support.config.FastJsonConfig;import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;/** * SpringBoot的启动类, 使用@SpringBootApplication修饰 * @author Lx * * 继承WebMvcConfigurerAdapter, 重写 * */@SpringBootApplicationpublic class Application extends WebMvcConfigurerAdapter{ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); /* * 1. 需要先定义一个convert转换器对象 * 2. 配置添加fastjson的配置信息, 比如: 是否要格式化返回的json数据; * 3. 把配置信息添加到convert转换器对象中; * 4. 解决中文乱码 * 5. 将convert添加到转换器对象当中; */ // 1. FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); // 2. FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); // 3. fastConverter.setFastJsonConfig(fastJsonConfig); //4. 解决中文乱码问题 List<MediaType> fastMediaTypes = new ArrayList<MediaType>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(fastMediaTypes); // 5. converters.add(fastConverter); }; public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
③ 在实体类中添加FastJson注解校验配置的FastJson是否生效
package online.bendou.springboot.fastjsondemo.entity;import java.util.Date;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import org.springframework.stereotype.Component;import com.alibaba.fastjson.annotation.JSONField;@Component@Entity(name="user_info")public class User { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Integer id; private String name; // 出生日期 @JSONField(format="yyyy-MM-dd") private Date birthday; get/set... }
④ 编写Contoller
package online.bendou.springboot.fastjsondemo.controller;import java.util.Date;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import online.bendou.springboot.fastjsondemo.entity.User;@RestControllerpublic class FastJsonController { @GetMapping("/user") public Object findAll(){ User user = new User(); user.setId(1); user.setName("张三"); user.setBirthday(new Date()); return user; }}
⑤ 运行启动类, 浏览器访问测试
方式二:
在Application.java启动类中注入Bean: HttpMessageConverters
package online.bendou.springboot.fastjsondemo;import java.util.ArrayList;import java.util.List;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.web.HttpMessageConverters;import org.springframework.context.annotation.Bean;import org.springframework.http.MediaType;import org.springframework.http.converter.HttpMessageConverter;import org.springframework.web.bind.annotation.RestController;import com.alibaba.fastjson.serializer.SerializerFeature;import com.alibaba.fastjson.support.config.FastJsonConfig;import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;/** * 配置FastJson的方式二: * 在启动类中注入HttpMessageConverters * @author Lx * */@RestControllerpublic class Application { @Bean public HttpMessageConverters fastJsonHttpMessageConverters(){ // 1.创建一个converter对象 FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); // 2.创建配置对象 FastJsonConfig fastJsonConfig = new FastJsonConfig(); // 3.添加配置 fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); // 4.将配置添加到转换器对象中 fastConverter.setFastJsonConfig(fastJsonConfig); // 5. 解决中文乱码问题 List<MediaType> mediaTypes = new ArrayList<MediaType>(); mediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(mediaTypes); // 6.将转换器对象转化为HttpMessageConverter对象 HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); } public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
阅读全文
0 0
- SpringBoot-01SpringBoot配置FastJson
- springboot配置fastjson
- SpringBoot配置FastJson并解决乱码问题
- springboot配置fastjson并解决乱码问题
- 2.SpringBoot-FastJson
- springboot使用fastjson
- SpringBoot中使用fastjson
- springboot使用fastjson
- springboot 整合fastjson
- SpringBoot 使用fastjson初识
- springboot 整合resteasy+fastjson
- springboot系列文章(四)-springboot使用fastjson
- SpringBoot--配置fastjson(日期格式转化)和热部署
- SpringBoot之fastjson(M)
- SpringBoot+FastJson 中文乱码问题
- springboot 系统学习2 -- fastjson
- fastjson在springboot中的使用
- Springboot 添加fastjson依赖包
- 最全面的常用正则表达式大全
- 数仓相关文章索引(1)
- 中国的振兴之路·《看懂世界格局的第一本书·2》
- SpringBoot中logback日志保存到mongoDB
- gpio-based i2c for eCos
- SpringBoot-01SpringBoot配置FastJson
- redis的基本操作
- git回退版本
- 微信本地开发准备
- 链表(链式存储)的基本操作
- SublimeText在Mac系统上的安装方法
- OrmLite数据库
- MYBATIS 的parameter
- ARM平台嵌入式Linux下使用3G/4G 模块