spring boot 中替换默认使用fastjson

  • 引入包

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

  • spring boot 默认使用的是json的jackson,这里替换成了fastjson

第一种:

package com.jf.mavenLogin;

import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
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!
 *
 */

@ComponentScan(basePackages = "com.jf.controller")

@EnableAutoConfiguration
public class App extends WebMvcConfigurerAdapter
{
    
    
    
    /**
     * spring boot 默认使用的是json的jackson,这里替换成了fastjson
     *  也可以采用注入bean的方式
     * @Description
     * @Param
     * @Return
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        
        //定义convert的转换对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        //添加fastjson的配置信息
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat
        );
        
        //把配置信息添加到对象中
        fastConverter.setFastJsonConfig(fastJsonConfig);
        
        //把新建的对象替换返回
        converters.add(fastConverter);
        
    }

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

 

 第二种:

package com.jf.mavenLogin;

import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
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!
 *
 */

@ComponentScan(basePackages = "com.jf.controller")

@EnableAutoConfiguration
public class AppBeanJson
{
    
    
    
    /**
     * spring boot 默认使用的是json的jackson,这里替换成了fastjson
     *  也可以采用注入bean的方式
     * @Description
     * @Param
     * @Return
     */
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        super.configureMessageConverters(converters);
//        
//        //定义convert的转换对象
//        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//        //添加fastjson的配置信息
//        FastJsonConfig fastJsonConfig = new FastJsonConfig();
//        fastJsonConfig.setSerializerFeatures(
//                SerializerFeature.PrettyFormat
//        );
//        
//        //把配置信息添加到对象中
//        fastConverter.setFastJsonConfig(fastJsonConfig);
//        
//        //把新建的对象替换返回
//        converters.add(fastConverter);
//        
//    }
    
    @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);
    }


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

 

 对象

package com.jf.entity;

import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;

/**
 * @Description
 * @Author zengzhiqiang
 * @Date 2019年1月28日
 */

public class Student {
    
    @JSONField(format="yyyy-MM-dd HH:mm")
    private  Date  timed;
    
    private String id ;

    public Date getTimed() {
        return timed;
    }

    public void setTimed(Date timed) {
        this.timed = timed;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    
    
    

}

 

 

package com.jf.controller;

import java.util.Date;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jf.entity.Student;

/**
 * @Description
 * @Author
 * @Date 2018年7月30日
 */
@RestController
public class HelloSpringBootController {
    

    @RequestMapping("/hello")
    public Student  hello(){
        
        Student s = new Student();
        s.setId("111");
        s.setTimed(new Date());    
        s.setDes("2222222222555");
        return s;
    }
    
}

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值