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.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.15</version>
    </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<HttpMessageConverter<?>> 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” }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值