@ResponseBody 将内容或对象作为 HTTP 响应正文返回,使用@ResponseBody将会跳过视图处理部分,而是调用适合HttpMessageConverter,将返回值写入输出流。
<mvc:annotation-driven />
开启了之后它给AnnotationMethodHandlerAdapter
初始化 7个转换器,
可以通过调用AnnotationMethodHandlerAdapter类的getMessageConverts()
方法来获取转换器的一个集合 List<HttpMessageConverter>
默认给AnnotationMethodHandlerAdapter初始化的有(当然我们也可以添加自定义的converter)
ByteArrayHttpMessageConverter
StringHttpMessageConverter
ResourceHttpMessageConverter
SourceHttpMessageConverter<T>
XmlAwareFormHttpMessageConverter
Jaxb2RootElementHttpMessageConverter
MappingJacksonHttpMessageConverter
客户端的请求header中寻找客户端可接收的类型
StringHttpMessageConverter
支持String , Accept所有类型MappingJacksonHttpMessageConverter
支持Map List 实体对象等等 ,Accept:application/json
解决办法
spring-mvc-3.0(包括)之前<mvc:annotation-driven/>下面不包含任何子元素
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property>
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<mvc:message-converters>
<mvc:annotation-driven>
MappingJackson2HttpMessageConverter 可以替换FastJsonHttpMessageConverter代替
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value><!-- 避免IE出现下载JSON文件的情况 -->
</list>
</property>
<property name="features">
<util:list>
<!-- <value>WriteMapNullValue</value> -->
<value>QuoteFieldNames</value>
<value>WriteDateUseDateFormat</value>
</util:list>
</property>
</bean>
spring-mvc
- spring-mvc-3.1.xsd annotation-driven
xsd:element name="annotation-driven">
<xsd:annotation>...</xsd:annotation>
<xsd:complexType>
<xsd:all minOccurs="0">
<xsd:element name="message-converters" minOccurs="0">...</xsd:element>
<xsd:element name="argument-resolvers" minOccurs="0">...</xsd:element>
<xsd:element name="return-value-handlers" minOccurs="0">...</xsd:element>
</xsd:all>
<xsd:attribute name="conversion-service" type="xsd:string">...</xsd:attribute>
<xsd:attribute name="validator" type="xsd:string">...</xsd:attribute>
<xsd:attribute name="message-codes-resolver" type="xsd:string">...</xsd:attribute>
<xsd:attribute name="ignoreDefaultModelOnRedirect" type="xsd:boolean">...</xsd:attribute>
</xsd:complexType>
</xsd:element>
- spring-mvc-3.0.xsd annotation-driven
<xsd:element name="annotation-driven">
<xsd:annotation>...</xsd:annotation>
<xsd:complexType>
<xsd:attribute name="conversion-service" type="xsd:string">...</xsd:attribute>
<xsd:attribute name="validator" type="xsd:string">... </xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:element> XSD元素
<xsd:attribute> XSD属性
<all> 指示器规定子元素可以按照任意顺序出现,且每个子元素必须只出现一次
<maxOccurs> 指示器可规定某个元素可出现的最大次数:
<minOccurs> 指示器可规定某个元素能够出现的最小次数:
最常用的类型是:type
xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time