zoukankan      html  css  js  c++  java
  • 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、测试

      格式化之前

      

      格式化之后

      

  • 相关阅读:
    Golang Gin使用模板及框架下返回Json
    Golang Web下返回HTML内容处理方法
    Golang net.http和Gin框架下的模板--View调用后台方法 --view接收后台数据的运用 及 嵌套模板和继承模板的使用
    设置oracle编辑的快捷方式
    oracle中的异常处理方法
    游标的使用
    网站登录界面包数据库异常
    在PLSQL中不能使用中文作为查询条件查询数据
    oracle错误一览表
    oracle导入时报错
  • 原文地址:https://www.cnblogs.com/studyDetail/p/6995228.html
Copyright © 2011-2022 走看看