Spring Boot入门——json数据处理

Posted Miss_wang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot入门——json数据处理相关的知识,希望对你有一定的参考价值。

1、引入fastJson插件

<!-- 引入fastjson插件 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.32</version>
    </dependency>
  
  <!-- 打包插件 -->
  <build>
      <plugins>
          <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
              <configuration>
                  <fork>true</fork><!-- 热部署生效必须加 -->
              </configuration>
          </plugin>
      </plugins>
  </build>

2、两种方法实现

  2.1、在App.java文件中实现HttpMessageConverters

 

  @Bean
    public HttpMessageConverters fastJsonConverters(){
        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastConf = new FastJsonConfig();
        
        fastConf.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastJsonConverter.setFastJsonConfig(fastConf);
        
        HttpMessageConverter<?> converter = fastJsonConverter;
        return new HttpMessageConverters(converter);    
    }

 

  2.2、在App.java类继承WebMvcConfigurerAdapter类,并重写configureMessageConverters方法

 

@SpringBootApplication
public class App extends WebMvcConfigurerAdapter{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        SpringApplication.run(App.class, args);
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // TODO Auto-generated method stub
        super.configureMessageConverters(converters);
        
        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastConf = new FastJsonConfig();
        
        fastConf.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastJsonConverter.setFastJsonConfig(fastConf);
        
        converters.add(fastJsonConverter);
    }
    
    
    /*@Bean
    public HttpMessageConverters fastJsonConverters(){
        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastConf = new FastJsonConfig();
        
        fastConf.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastJsonConverter.setFastJsonConfig(fastConf);
        
        HttpMessageConverter<?> converter = fastJsonConverter;
        return new HttpMessageConverters(converter);    
    }*/
    
    
}

3、格式化属性的值

 

    private String userId;
    private String userName;
    
    @JSONField(format="yyyy-MM-dd")
    private Date createDate;    

 

4、测试

  格式化之前

  

  格式化之后

  

 

以上是关于Spring Boot入门——json数据处理的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 从入门到精通整合 MongoDB 实现读写非关系型数据库

Spring Boot2(015):JSON

spring boot?Swagger2文档构建及单元测试

262.Spring Boot JWT:入门教程

通过使用 Spring Boot 插入来自循环的所有数据来创建 JSON 数组

使用Spring Boot JPA Specification实现使用JSON数据来查询实体数据