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

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旷野历程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值