Using third-party JSON framework for getting started with Spring Boot

Posted by e39m5 on Sun, 05 Apr 2020 10:22:39 +0200

Note summaries

This paper takes Spring Boot using fastjson instead of the original JSON framework as an example to show how Spring Boot uses the third-party JSON framework.

Spring Boot uses custom JSON framework methods

Method 1

  1. Introducing dependency into pom.xml;
  2. Inherit WebMvcConfigurerAdapter from the App class and overload the configureMessageConverters method to add a custom JSON parsing framework.

Method two

  1. Introducing dependency into pom.xml;
  2. Use @ Bean to introduce a third-party framework.

Experimental process

Experiment process 1:

Created with Rapha ë l 2.1.2 starts to configure pom.xml file, introduces dependency package, inherits WebMvcConfigurerAdapter method in App class, overloads configureMessageConverters method, writes GetJsonDemo class to generate JSON string, modifies MyController class, and adds getDemo method to end

Experiment process 2:

Created with Rapha ë l 2.1.2 starts to configure pom.xml file, introduces dependency package to inject Bean: HttpMessageConverters into App class, writes GetJsonDemo class to generate JSON string, modifies MyController class, and adds getDemo method to end

Experimental code

pom.xml

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.15</version>
    </dependency>

Method 1 App code

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.Define convert to transform the message object;
         * 2.Add fastjson configuration information;
         * 3.Add configuration information to convert;
         * 4.Add convert to 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);
    }
}

Method 2 App code

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 class code

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 class code

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

experimental result

Visit http://localhost:8080/getJsonDemoData , browser feedback
{ "createTime":"18-03-06 20:48", "id":1, "name":"JsonDemoData" }

Topics: Spring JSON xml Java