Spring Boot入门之使用第三方JSON框架

笔记摘要

本文以Spring Boot使用fastjson替代原有JSON框架为例,介绍Spring Boot如何使用第三方JSON框架。

Spring Boot使用自定义JSON框架方法

方法一

  1. 在pom.xml中引入相关依赖;
  2. 在App类中继承WebMvcConfigurerAdapter并重载configureMessageConverters方法,增加自定义的JSON解析框架。

方法二

  1. 在pom.xml中引入相关依赖;
  2. 使用@Bean引入第三方框架。

实验过程

实验过程一:

Created with Raphaël 2.1.2 开始 配置pom.xml文件,引入依赖包 在App类中继承WebMvcConfigurerAdapter方法 重载configureMessageConverters方法 编写GetJsonDemo类用于生成JSON字符串 修改MyController类,增加getDemo方法 结束

实验过程二:

Created with Raphaël 2.1.2 开始 配置pom.xml文件,引入依赖包 在App类中注入Bean:HttpMessageConverters 编写GetJsonDemo类用于生成JSON字符串 修改MyController类,增加getDemo方法 结束

实验代码

pom.xml

    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>fastjsonartifactId>
      <version>1.2.15version>
    dependency>

方法一App类代码

package my.spring.helloworld;

import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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;

@SpringBootApplication
public class App extends WebMvcConfigurerAdapter
{

    @Override
    public void configureMessageConverters(List> converters) {
        super.configureMessageConverters(converters);

        /*
         * 1.定义convert,转换消息的对象;
         * 2.添加fastjson配置信息;
         * 3.在convert中添加配置信息;
         * 4.将convert添加到converters。
         */
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);

        converters.add(fastJsonHttpMessageConverter);
    }

    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }
}

方法二App类代码

package my.spring.helloworld;

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;

@SpringBootApplication
public class App
{
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);

        HttpMessageConverter converter = fastJsonHttpMessageConverter;
        return new HttpMessageConverters(converter);
    }

    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }
}

GetJsonDemo类代码

package my.spring.helloworld;

import java.util.Date;
import com.alibaba.fastjson.annotation.JSONField;

public class GetJsonDemo {
    private int id;
    private String name;
    @JSONField(format="yy-MM-dd HH:mm")
    private Date createTime;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}

MyController类代码

    @RequestMapping("/getJsonDemoData")
    @ResponseBody
    public GetJsonDemo getDemo() {
        GetJsonDemo demo = new GetJsonDemo();
        demo.setId(1);
        demo.setName("JsonDemoData");
        demo.setCreateTime(new Date());
        return demo;
    }

实验结果

访问http://localhost:8080/getJsonDemoData,浏览器反馈
{ “createTime”:”18-03-06 20:48”, “id”:1, “name”:”JsonDemoData” }

你可能感兴趣的:(SpringBoot,spring,boot)