(3)spring boot如何使用第三方json解析框架
2017-03-01 16:15
1006 查看
1. 在pom.xml文件中,加入第三方json依赖jar包
这里以使用fastjson为例,pom.xml内容如下:
2. 修改启动类
两种方法代码实例:
通过启动类中任意一种方法,均可以实现引入第三方json解析框架,替代掉spring boot默认的jackson解析框架。
这里以使用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! */ @SpringBootApplication public 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解析框架。
相关文章推荐
- springboot使用第三方的json解析框架fastjson
- SpringBoot 之 第三方Json解析框架FastJson的使用
- Spring Boot入门之使用第三方JSON框架
- Spring Boot 使用第三方json解析 : fastjson
- 4. 使用别的json解析框架【从零开始学Spring Boot】
- 【spring boot笔记】 springboot 使用第三方json解析
- (4)Spring Boot使用别的json解析框架【从零开始学Spring Boot】
- 4. 使用别的json解析框架【从零开始学Spring Boot】
- (二)初识springboot之使用fastjson解析数据(解决乱码及简单配置)
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- Spring Boot 学习4--完美使用FastJson解析Json数据
- 【传智播客郑州校区分享】Spring Boot中使用FastJson解析Json数据
- Spring-boot中使用fastjson作为json框架
- SpringBoot学习:Spring Boot使用FastJson解析JSON数据
- 关于怎么解决从ajax传入的json参数注入到Controller的接收对象 以及如何在Spring Boot项目使用参数校验
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- spring boot使用fastJson解析数据
- spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- springboot 使用hibernate进行双向关联json解析时死循环处理