您的位置:首页 > 编程语言 > Java开发

(3)spring boot如何使用第三方json解析框架

2017-03-01 16:15 1006 查看
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!
 */
@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解析框架。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: