码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot 初体验(2)——FastJson

时间:2018-10-23 22:59:10      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:ram   inf   spl   import   技术   get   user   conf   tpm   

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.启动测试效果

技术分享图片

 

Spring Boot 初体验(2)——FastJson

标签:ram   inf   spl   import   技术   get   user   conf   tpm   

原文地址:https://www.cnblogs.com/kuangyefeige/p/9839532.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!