1.pom文件中加入fastJson依赖版本要求 1.2.10+

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
View Code

2。修改启动类的方法

方法一:

 (1)启动类继承extends WebMvcConfigurerAdapter

 (2)覆盖方法configureMessageConverters

package com.mt;

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);
        FastJsonHttpMessageConverter convvert =
            new FastJsonHttpMessageConverter();
        FastJsonConfig jsonConfig = new FastJsonConfig();
        jsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        convvert.setFastJsonConfig(jsonConfig);
        converters.add(convvert);
        
    }
    
    public static void main(String[] args)
    {
        System.out.println("helloSpring Boot");
        SpringApplication.run(App.class, args);
        
    }
    
}
View Code

方法二:

(1)在App.java启动类中, 注入Bean : HttpMessageConverters

package com.mt;

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 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);
     * FastJsonHttpMessageConverter convvert = new
     * FastJsonHttpMessageConverter(); FastJsonConfig jsonConfig = new
     * FastJsonConfig();
     * jsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
     * convvert.setFastJsonConfig(jsonConfig); converters.add(convvert);
     * 
     * }
     */
    public static void main(String[] args)
    {
        System.out.println("helloSpring Boot");
        SpringApplication.run(App.class, args);
        
    }
    
    // 方法二:
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters()
    {
        FastJsonHttpMessageConverter fastConverter =
            new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }
    
}
View Code

3.使用fastjson实体类加注解使用Fastjson

package com.mt.pojo;

import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;

public class User
{
    private Long id;
    
    private String name;
    
    private String pwd;
    
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date creatTime;
    
    public User(Long id, String name, String pwd, Date creatTime)
    {
        super();
        this.id = id;
        this.name = name;
        this.pwd = pwd;
        this.creatTime = creatTime;
    }
    
    public Date getCreatTime()
    {
        return creatTime;
    }
    
    public void setCreatTime(Date creatTime)
    {
        this.creatTime = creatTime;
    }
    
    public Long getId()
    {
        return id;
    }
    
    public void setId(Long id)
    {
        this.id = id;
    }
    
    public String getName()
    {
        return name;
    }
    
    public void setName(String name)
    {
        this.name = name;
    }
    
    public String getPwd()
    {
        return pwd;
    }
    
    public void setPwd(String pwd)
    {
        this.pwd = pwd;
    }
    
    public User(Long id, String name, String pwd)
    {
        super();
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }
    
    public User()
    {
        super();
    }
    
}
View Code

4.启动测试效果