(3)spring boot如何使用第三方json解析框架
来源:互联网 发布:试用软件过期怎么办 编辑:程序博客网 时间:2024/05/22 09:20
1. 在pom.xml文件中,加入第三方json依赖jar包
这里以使用fastjson为例,pom.xml内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lanhuigu</groupId> <artifactId>spring-boot-hello</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-hello</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- 配置jdk版本,默认为1.6 --> <java.version>1.8.0_66</java.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- spring boot引入相关web开发jar包,包括spring,spring mvc,spring boot,json框架等等 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 这个地方不需要指定jar包版本,根据parent配置自动选择 --> <!-- <version>1.4.1.RELEASE</version> --></dependency><dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version></dependency> </dependencies></project>
2. 修改启动类
两种方法代码实例:
package com.lanhuigu;import java.util.List;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.web.HttpMessageConverters;import org.springframework.context.annotation.Bean;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;/** * Hello world! */@SpringBootApplicationpublic class App extends WebMvcConfigurerAdapter{ /* * spring boot中如何使用fastjson框架,而不是使用默认jackson框架解析json数据 */ // ###########################方法一########################## /* * (1)启动类继承于WebMvcConfigurerAdapter * (2)覆盖方法configureMessageConverters */ // @Override // public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { // super.configureMessageConverters(converters); // /* // * 1.需要先定义一个converters转换消息的对象 // * 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据 // * 3.在converter中添加配置信息 // * 4.将converter添加到converters信息中 // */ // // 1.需要先定义一个converters转换消息的对象 // FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); // // // 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据 // FastJsonConfig fastJsonConfig = new FastJsonConfig(); // fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); // // // 3.在converter中添加配置信息 // fastConverter.setFastJsonConfig(fastJsonConfig); // // // 4.将converter添加到converters信息中 // converters.add(fastConverter); // } // ###########################方法二########################## /* * 注入bean: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 ) { /*System.out.println( "Hello World!" );*/ SpringApplication.run(App.class, args); }}
通过启动类中任意一种方法,均可以实现引入第三方json解析框架,替代掉spring boot默认的jackson解析框架。
0 0
- (3)spring boot如何使用第三方json解析框架
- Spring Boot 使用第三方json解析 : fastjson
- SpringBoot 之 第三方Json解析框架FastJson的使用
- springboot使用第三方的json解析框架fastjson
- 4. 使用别的json解析框架【从零开始学Spring Boot】
- qt解析json(使用第三方库
- Spring Boot 使用json框架fastjson
- Spring Boot使用FastJson解析JSON数据
- spring boot:使用fastJson解析json数据
- Android-第三方框架-json解析(FastJson,GJson)
- Spring Boot学习--使用本地/第三方jar
- MJExtension json解析第三方
- spring boot 框架解析
- c++ 第三方json解析库 jsoncpp的使用
- qt解析json(使用第三方库)
- 关于解析json— 使用第三方库Gson
- c++ 第三方json解析库 jsoncpp的使用
- IOS 使用第三方框架(DOM)解析XML(GDataXML)
- 读书笔记--敏捷软件开发(1)
- GBK,UTF-8,和ISO8859-1之间的编码与解码
- 如何找到占用CPU和内存最高的线程
- TI CC2640R2F的开发工具与软件,据说可用C语言实现任务算法?
- Android Selector 背景选择器
- (3)spring boot如何使用第三方json解析框架
- Redis linux安装
- phpcms表单向导设置
- 【Oracle学习】之 在sqlplus设置格式 和 case...when 和 decode
- Dns缓存问题(转载)
- Mean ± SEM or Mean(SD)
- js易错整理
- Genymotion VirtualBox COM server is not running or failed to start.
- IOS swift版 sqlite3详解