zoukankan      html  css  js  c++  java
  • Spring Boot 初体验(2)——FastJson

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

    一个走投无路的庄稼汉
  • 相关阅读:
    OSG中的示例程序简介(转)
    空间点到直线垂足坐标的解算方法 (转)
    OpenscenGraph中控制swapbuffer的方法(用于多机大屏幕同步显示机制)
    吏治 ? 官治 ?
    C++中使用union的几点思考(转)
    一个穷人移民美国三年的生活经历(转)
    展望99股市:谁是重组大黑马?(转)
    mysql 在一个实例运行情况下再搭建一个实例
    在CentOS下安装crontab服务
    Zabbix监控之迁移zabbix server
  • 原文地址:https://www.cnblogs.com/kuangyefeige/p/9839532.html
Copyright © 2011-2022 走看看