Spring Mvc 3.1 之后如何配置messageConverters

网址:http://blog.csdn.net/jiesa/article/details/54912534

<mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。

这句话我在很多帖子都看到过,我自己的项目本身使用的Spring MVC 3.2,实际上在3.1之后,<mvc:annotation-driven />注册的类发生了变化

Spring Framework 3.1 introduces a new set of support classes for processing requests with annotated controllers:

  • RequestMappingHandlerMapping

  • RequestMappingHandlerAdapter

  • ExceptionHandlerExceptionResolver

These classes are a replacement for the existing:

  • DefaultAnnotationHandlerMapping

  • AnnotationMethodHandlerAdapter

  • AnnotationMethodHandlerExceptionResolver

[html] view plaincopy

<embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=16&height=16" wmode="transparent" style="box-sizing: border-box;">

[图片上传失败...(image-258a92-1519365200909)]

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:mvc="http://www.springframework.org/schema/mvc"

  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  5. xsi:schemaLocation="

  6. http://www.springframework.org/schema/beans

  7. http://www.springframework.org/schema/beans/spring-beans.xsd

  8. http://www.springframework.org/schema/mvc

  9. http://www.springframework.org/schema/mvc/spring-mvc.xsd">

  10. <mvc:annotation-driven />

  11. </beans>

The above registers a RequestMappingHandlerMapping, a RequestMappingHandlerAdapter, and an ExceptionHandlerExceptionResolver (among others) in support of processing requests with annotated controller methods using annotations such as @RequestMapping, @ExceptionHandler, and others.

It also enables the following:

  1. Spring 3 style type conversion through a ConversionService instance in addition to the JavaBeans PropertyEditors used for Data Binding.

  2. Support for formatting Number fields using the @NumberFormat annotation through the ConversionService.

  3. Support for formatting Date, Calendar, Long, and Joda Time fields using the @DateTimeFormat annotation.

  4. Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is present on the classpath.

  5. HttpMessageConverter support for @RequestBody method parameters and @ResponseBody method return values from @RequestMapping or @ExceptionHandler methods.

    This is the complete list of HttpMessageConverters set up by mvc:annotation-driven:

    • ByteArrayHttpMessageConverter converts byte arrays.

    • StringHttpMessageConverter converts strings.

    • ResourceHttpMessageConverter converts to/from org.springframework.core.io.Resource for all media types.

    • SourceHttpMessageConverter converts to/from a javax.xml.transform.Source.

    • FormHttpMessageConverter converts form data to/from a MultiValueMap<String, String>.

    • Jaxb2RootElementHttpMessageConverter converts Java objects to/from XML — added if JAXB2 is present on the classpath.

    • MappingJackson2HttpMessageConverter (or MappingJacksonHttpMessageConverter) converts to/from JSON — added if Jackson 2 (or Jackson) is present on the classpath.

    • AtomFeedHttpMessageConverter converts Atom feeds — added if Rome is present on the classpath.

    • RssChannelHttpMessageConverter converts RSS feeds — added if Rome is present on the classpath.

这是摘取的官方文档,可以看出,注册的类已经变成了RequestMappingHandlerMapping和 RequestMappingHandlerAdapter。

我之前在不知道的时候,使用AnnotationMethodHandlerAdapter 进行配置,结果在有<mvc:annotation-driven />存在的情况下,我自己配置的AnnotationMethodHandlerAdapter 怎么都不起作用,于是去掉了<mvc:annotation-driven />标签,手动注册了AnnotationMethodHandlerAdapter ,和DefaultAnnotationHandlerMapping。结果引发了其他问题,比如文件无法上传的问题。

阅读文档发现Spring提供了基于<mvc:annotation-driven />自定义messageConverters的方法,如下所示:

[html] view plaincopy

<embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=16&height=16" wmode="transparent" style="box-sizing: border-box;">

[图片上传失败...(image-f260ff-1519365200908)]

  1. <mvc:annotation-driven conversion-service="conversionService">
  2. <mvc:message-converters>
  3. <bean class="org.example.MyHttpMessageConverter"/>
  4. <bean class="org.example.MyOtherHttpMessageConverter"/>
  5. </mvc:message-converters>
  6. </mvc:annotation-driven>

下面展示我自己的配置

[html] view plaincopy

<embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=16&height=16" wmode="transparent" style="box-sizing: border-box;">

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"

  4. xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

  5. xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:mvc="http://www.springframework.org/schema/mvc"

  6. xmlns:util="http://www.springframework.org/schema/util"

  7. xsi:schemaLocation="http://www.springframework.org/schema/beans

  8. http://www.springframework.org/schema/beans/spring-beans.xsd

  9. http://www.springframework.org/schema/tx

  10. http://www.springframework.org/schema/tx/spring-tx.xsd

  11. http://www.springframework.org/schema/context

  12. http://www.springframework.org/schema/context/spring-context.xsd

  13. http://www.springframework.org/schema/aop

  14. http://www.springframework.org/schema/aop/spring-aop.xsd

  15. http://www.springframework.org/schema/jdbc

  16. http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd

  17. http://www.springframework.org/schema/util

  18. http://www.springframework.org/schema/util/spring-util-3.2.xsd

  19. http://www.springframework.org/schema/mvc

  20. http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

  21. <context:component-scan base-package="cn.xx.xx"

  22. use-default-filters="false">

  23. <context:include-filter expression="org.springframework.stereotype.Controller"

  24. type="annotation" />

  25. </context:component-scan>

  26. <mvc:annotation-driven>

  27. <mvc:message-converters>

  28. <ref bean="stringHttpMessageConverter" />

  29. <ref bean="fastJsonHttpMessageConverter" />

  30. <ref bean="marshallingHttpMessageConverter" />

  31. </mvc:message-converters>

  32. </mvc:annotation-driven>

  33. <bean id="stringHttpMessageConverter"

  34. class="org.springframework.http.converter.StringHttpMessageConverter">

  35. <property name="supportedMediaTypes">

  36. <list>

  37. <value>text/plain;charset=UTF-8</value>

  38. </list>

  39. </property>

  40. </bean>

  41. <bean id="fastJsonHttpMessageConverter"

  42. class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">

  43. <property name="supportedMediaTypes">

  44. <list>

  45. <value>application/json;charset=UTF-8</value>

  46. <value>text/html;charset=UTF-8</value>

  47. </list>

  48. </property>

  49. <property name="features">

  50. <util:list>

  51. <value>QuoteFieldNames</value>

  52. <value>WriteDateUseDateFormat</value>

  53. </util:list>

  54. </property>

  55. </bean>

  56. <bean id="marshallingHttpMessageConverter"

  57. class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">

  58. <property name="marshaller" ref="castorMarshaller" />

  59. <property name="unmarshaller" ref="castorMarshaller" />

  60. <property name="supportedMediaTypes">

  61. <list>

  62. <value>text/xml;charset=UTF-8</value>

  63. <value>application/xml;charset=UTF-8</value>

  64. </list>

  65. </property>

  66. </bean>

  67. <util:list id="messageConverters">

  68. <ref bean="stringHttpMessageConverter" />

  69. <ref bean="fastJsonHttpMessageConverter" />

  70. <ref bean="marshallingHttpMessageConverter" />

  71. </util:list>

  72. <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller" />

  73. <aop:aspectj-autoproxy />

  74. <mvc:resources location="/" mapping="/*/.html" order="0" />

  75. <mvc:resources location="/images/" mapping="/images/**" />

  76. <mvc:resources location="/img/" mapping="/img/**" />

  77. <mvc:resources location="/download/" mapping="/download/**" />

  78. <mvc:resources location="/js/" mapping="/js/**" />

  79. <mvc:resources location="/css/" mapping="/css/**" />

  80. <mvc:resources location="/plugin/" mapping="/plugin/**" />

  81. <mvc:resources location="/WEB-INF/pages/" mapping="/pages/**" />

  82. <bean id="messageSource"

  83. class="org.springframework.context.support.ResourceBundleMessageSource">

  84. <property name="basename" value="messages"></property>

  85. </bean>

  86. <bean

  87. class="org.springframework.web.servlet.view.InternalResourceViewResolver">

  88. <property name="prefix" value="/WEB-INF/pages/" />

  89. <property name="suffix" value=".jsp" />

  90. </bean>

  91. <bean id="multipartResolver"

  92. class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

  93. <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">

  94. <property name="messageConverters" ref="messageConverters">

  95. </property>

  96. </bean>

  97. </beans>

http://blog.csdn.net/yfisaboy/article/details/31755631

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,053评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,527评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,779评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,685评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,699评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,609评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,989评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,654评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,890评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,634评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,716评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,394评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,976评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,950评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,191评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,849评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,458评论 2 342

推荐阅读更多精彩内容