spring mvc整合fastjosn配置文件

今天想在自己写的项目里面整合一下阿里的fastjson,可能会有同学问我,为什么不用自带的jackson呢?因为fastjson具备了一些jackson不具备的能力,执行效率相对快一些。

我先贴一下我的spring mvc和fastjson版本。

    
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <spring.version>4.3.7.RELEASEspring.version>
    properties>
    <dependencies>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aopartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>${spring.version}version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-ormartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-context-supportartifactId>
            <version>${spring.version}version>
        dependency>
                
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.4version>
        dependency>
        dependencies>

下面是spring mvc的配置文件中,fastjson这一块的配置

    
    <mvc:annotation-driven>
              
        <mvc:message-converters register-defaults="true">

             <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                  <property name="supportedMediaTypes" value="text/html;charset=UTF-8" />
                  <property name="features">
                    <list>
                    
                    <value>QuoteFieldNamesvalue>
                     
                    <value>WriteMapNullValuevalue>
                    
                    <value>WriteNullBooleanAsFalsevalue>
                     
                    <value>WriteNullStringAsEmptyvalue>
                    list>
                  property>
             bean>
        mvc:message-converters>   
    mvc:annotation-driven>

在上面的配置代码中,我发现了,FastJsonHttpMessageConverter与StringHttpMessageConverter一样,都继承了org.springframework.http.converter.AbstractHttpMessageConverter这个类,而我们设置的supportedMediaTypes这个参数就是从AbstractHttpMessageConverter这里继承的。
spring mvc整合fastjosn配置文件_第1张图片

与StringHttpMessageConverter不同的是,FastJsonHttpMessageConverter里面整合了一些自己的东西,比如上面的features参数,以及默认定义的Charset为UTF8。

学无止境,生生不息。

你可能感兴趣的:(spring)