zoukankan      html  css  js  c++  java
  • springboot使用阿里fastjson来解析数据

    1.spring boot默认使用的json解析框架是jackson,使用fastjson需要配置,首先引入fastjson依赖 
    pom.xml配置如下:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>spring.boot.helloworld</groupId>
      <artifactId>helloworld</artifactId>
      <packaging>war</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>helloworld Maven Webapp</name>
      <url>http://maven.apache.org</url>
     
      <!--
      spring boot父节点依赖,引入这个之后相关的引入就不需要加version配置,spring boot
      会自动选择最合适的版本进行添加
      -->
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
      </parent>
     
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
      </properties>
     
      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
      <!-- fastjson依赖 -->
        <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.2.15</version>
        </dependency>
    </dependencies>
      <!--构建节点-->
      <build>
        <finalName>helloworld</finalName>
     
      </build>
    </project>

    2.配置有两种方式,一种是继承WebMvcConfigurerAdapter重写方法,另一种是@Bean注入第三方的json解析框架。 

    (1)继承WebMvcConfigurerAdapter

    /**
     * Created by struggle on 2017/7/29.
     */
    @SpringBootApplication//指定这是一个Spring boot应用程序
    public class App extends WebMvcConfigurerAdapter{
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            super.configureMessageConverters(converters);
            /*
            1.需要先定义一个convert转换消息的对象;
            2.添加fastjson的配置信息,比如是否要格式化返回的json数据
            3.在convert中添加配置信息
            4.将convert添加到converters中
             */
            //1.定义一个convert转换消息对象
            FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
            //2.添加fastjson的配置信息,比如:是否要格式化返回json数据
            FastJsonConfig fastJsonConfig=new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(
                    SerializerFeature.PrettyFormat
            );
            fastConverter.setFastJsonConfig(fastJsonConfig);
            converters.add(fastConverter);
        }
     
        public static void main(String[] args) {
            /**
             * 在main方法中启动应用程序
             */
            SpringApplication.run(App.class,args);
        }
    }

    (2)使用@Bean注入方式

    /**
     * Created by struggle on 2017/7/29.
     */
    @SpringBootApplication//指定这是一个Spring boot应用程序
    public class App{
     
        @Bean//使用@Bean注入fastJsonHttpMessageConvert
        public HttpMessageConverters fastJsonHttpMessageConverters(){
            //1.需要定义一个Convert转换消息的对象
            FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
            //2.添加fastjson的配置信息,比如是否要格式化返回的json数据
    //
            FastJsonConfig fastJsonConfig=new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
            //3.在convert中添加配置信息
            fastConverter.setFastJsonConfig(fastJsonConfig);
     
            HttpMessageConverter<?> converter=fastConverter;
            return new HttpMessageConverters(converter);
        }
        public static void main(String[] args) {
            /**
             * 在main方法中启动应用程序
             */
            SpringApplication.run(App.class,args);
        }
    }
  • 相关阅读:
    CocoaPods入门到精通
    SDAutoLayout 一行代码搞定自动布局
    iOS 开发实践之Auto Layout(From Vincent Sit)
    web前端开发_清除浮动
    转 使用Autolayout xib实现动态高度的TableViewCell
    有了Auto Layout,为什么你还是害怕写UITabelView的自适应布局?
    Objective-C 相关Category2
    Objective-C 相关Category
    Mac 破解Adobe Photoshop CS6
    leetcode@ [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)
  • 原文地址:https://www.cnblogs.com/chenmz1995/p/11386324.html
Copyright © 2011-2022 走看看