天天看点

SpringMVC配置返回结果类型

                                               分享知识 传递快乐

SpringMVC + FastJson

<mvc:annotation-driven>
  <!-- 将Jackson2HttpMessageConverter的默认格式化输出为true -->
  <mvc:message-converters register-defaults="true">
    <ref bean="fastJsonHttpMessageConverter" />
  </mvc:message-converters>
</mvc:annotation-driven>


<!-- 配置Fastjson支持 -->
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
  <property name="supportedMediaTypes">
    <list>
      <value>text/html;charset=UTF-8</value>
      <value>application/json</value>
    </list>
  </property>
  <property name="features">
    <list>
      <!-- 输出key时是否使用双引号 -->
      <value>QuoteFieldNames</value>
      <!-- 是否输出值为null的字段 -->
      <!-- <value>WriteMapNullValue</value> -->
      <!-- 数值字段如果为null,输出为0,而非null -->
      <value>WriteNullNumberAsZero</value>
      <!-- List字段如果为null,输出为[],而非null -->
      <value>WriteNullListAsEmpty</value>
      <!-- 字符类型字段如果为null,输出为"",而非null -->
      <value>WriteNullStringAsEmpty</value>
      <!-- Boolean字段如果为null,输出为false,而非null -->
      <value>WriteNullBooleanAsFalse</value>
      <!-- null String不输出 -->
      <value>WriteNullStringAsEmpty</value>
      <!-- null String也要输出 -->
      <!-- <value>WriteMapNullValue</value> -->
      <!-- Date的日期转换器 -->
      <value>WriteDateUseDateFormat</value>
    </list>
  </property>
</bean>      

SpringMVC+FastJson 扩展配置,根据URL后缀自动判定Content-Type及相应的视图

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
  <!-- 将Jackson2HttpMessageConverter的默认格式化输出为true -->
  <mvc:message-converters register-defaults="true">
    <!-- 配置Fastjson支持 -->
    <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
      <property name="supportedMediaTypes">
        <list>
          <value>text/html;charset=UTF-8</value>
          <value>application/json</value>
        </list>
      </property>
      <property name="features">
        <list>
          <!-- 输出key时是否使用双引号 -->
          <value>QuoteFieldNames</value>
          <!-- 是否输出值为null的字段 -->
          <!-- <value>WriteMapNullValue</value> -->
          <!-- 数值字段如果为null,输出为0,而非null -->
          <value>WriteNullNumberAsZero</value>
          <!-- List字段如果为null,输出为[],而非null -->
          <value>WriteNullListAsEmpty</value>
          <!-- 字符类型字段如果为null,输出为"",而非null -->
          <value>WriteNullStringAsEmpty</value>
          <!-- Boolean字段如果为null,输出为false,而非null -->
          <value>WriteNullBooleanAsFalse</value>
          <!-- null String不输出 -->
          <value>WriteNullStringAsEmpty</value>
          <!-- null String也要输出 -->
          <!-- <value>WriteMapNullValue</value> -->
          <!-- Date的日期转换器 -->
          <value>WriteDateUseDateFormat</value>
        </list>
      </property>
    </bean>
  </mvc:message-converters>
</mvc:annotation-driven>

<!-- 根据URL后缀自动判定Content-Type及相应的View -->
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
  <property name="mediaTypes">
    <map>
      <entry key="json" value="application/json" />
    </map>
  </property>
  <!-- 这里是否忽略掉accept header,默认就是false -->
  <property name="ignoreAcceptHeader" value="true" />
  <property name="favorPathExtension" value="true" />
</bean>      

使用fastjson的<value>WriteDateUseDateFormat</value>配置(使得返回的日期类型默认为yyyy-MM-dd hh:mm:ss), 特殊类型使用字段@JSONField来进行控制。

SpringMVC+MappingJackson2HttpMessageConverter

<mvc:annotation-driven>
  <mvc:message-converters>
    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
      <property name="objectMapper">
        <bean class="com.fasterxml.jackson.databind.ObjectMapper">
          <!-- 处理responseBody 里面日期类型 -->
          <property name="dateFormat">
            <bean class="java.text.SimpleDateFormat">
              <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
            </bean>
          </property>
          <!-- 为null字段时不显示 -->
          <property name="serializationInclusion">
            <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
          </property>
          <!-- 把所有的类属性名称都转换为小写, 同时单词(驼峰命令法)之间使用‘_‘字符来分割(一般用不上)。不过也可以在实体类上用注解的方式解决: -->
          <!-- 指定类:@JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class) -->
          <!-- 指定类属性:@JsonProperty(value="user_id") -->
          <property name="propertyNamingStrategy">
            <bean class="com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy" />
          </property>
        </bean>
      </property>
      <!-- 请求信息转换器,负责读取和写入json格式的数据 -->
      <property name="supportedMediaTypes">
        <list>
          <value>text/html;charset=UTF-8</value>
          <value>application/json; charset=UTF-8</value>
        </list>
      </property>
    </bean>
  </mvc:message-converters>
</mvc:annotation-driven>      

如果不想使用<mvc:annotation-driven>......</mvc:annotation-driven>的话,我们可以在重新定义个bean,指向的类为:AnnotationMethodHandlerAdapter

<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  <property name="messageConverters">
    <list>
      <!-- json转换器 -->
      <ref bean="fastJsonHttpMessageConverter" />
      <!-- 这个转换器可以解决返回字符串带引号的问题 -->
      <!-- <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> -->
    </list>
  </property>
</bean      

AnnotationMethodHandlerAdapter:通过注解,把一个URL映射到Controller类的方法上。

PS:

HandlerMapping接口的实现类:

SimpleUrlHandlerMapping:通过配置文件,把一个URL映射到Controller。

DefaultAnnotationHandlerMapping:通过注解,把一个URL映射到Controller类上。

HandlerAdapter接口:处理请求的映射

AnnotationMethodHandlerAdapter:通过注解,把一个URL映射到Controller类的方法上。

<mvc:annotation-driven>......</mvc:annotation-driven>标签的使用相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean。

避免IE执行AJAX时,返回JSON出现下载文件

<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  <property name="supportedMediaTypes">
    <list>
      <value>text/plain;charset=UTF-8</value>
      <value>text/html;charset=UTF-8</value>
    </list>
  </property>
  <property name="objectMapper">
    <bean class="com.fasterxml.jackson.databind.ObjectMapper">
      <!-- 处理responseBody 里面日期类型 -->
      <property name="dateFormat">
        <bean class="java.text.SimpleDateFormat">
          <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
        </bean>
      </property>
      <!-- 时区指定 -->
      <property name="timeZone" value="GMT+8" />
      <!-- 为null字段时不显示 -->
      <property name="serializationInclusion">
        <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
      </property>

    </bean>
  </property>
</bean>

<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  <property name="messageConverters">
    <list>
      <!-- json转换器 -->
      <ref bean="mappingJacksonHttpMessageConverter" />
      <!-- 这个转换器可以解决返回字符串带引号的问题 -->
      <!-- <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> -->
    </list>
  </property>
</bean      

这种是将字段为null的清理掉不在结果Json中显示出来,其余的还有以下几种配置:ALWAYS,NON_NULL,NON_DEFAULT,NON_EMPTY