一.DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter 的使用已经过时!

spring 3.1 开始我们应该用

RequestMappingHandlerMapping 来替换 DefaultAnnotationHandlerMapping,

用 RequestMappingHandlerAdapter 来替换 AnnotationMethodHandlerAdapter。

二.annotation-deiver详解

<mvc:annotation-driven /> 会自动注册RequestMappingHandlerMapping、RequestMappingHandlerAdapter 与xceptionHandlerExceptionResolver 三个bean。

还将提供以下支持:

  1. 支持使用 ConversionService 实例对表单参数进行类型转换;
  2. 支持使用 @NumberFormat annotation、@DateTimeFormat;
  3. 注解完成数据类型的格式化;
  4. 支持使用 @Valid 注解对 JavaBean 实例进行 JSR 303 验证;
  5. 支持使用 @RequestBody 和 @ResponseBody 注解;
mvc:annotation-driven作用:
  1. 当使用mvc:view-controller标签时一定要加入mvc:annotation-driven,不然会使requestMapping失效。
  2. 当为了处理静态资源问题而加入mvc:default-servlet-handler时,也一定要加入mvc:annotation-driven,不然requestMapping同样会失效。
  3. 当使用自定义类型转换器的时候需要加上mvc:annotation-driven标签。

三.手动配置驱动和JSON,不使用驱动注解MVC:annotation-deiver

手动配置,不使用<mvc:annotation-driven />

设计:手工指定RequestMappingHandlerMapping和RequestMappingHandlerAdapter,并给RequestMappingHandlerAdapter的messageConverters的注入属性值.不使用驱动注解(<mvc:annotation-driven />)自动配置的原因是:自动配置我没找到方法来修改response的Content-Type, 而自动配置默认的content-type是application/json;charset=UTF-8.这个contentType在谷歌浏览器很正常解析,而到了IE解析为弹出下载了,IE10,IE11一样不给面子!!!如果将contentType改为:text/html;charset=UTF-8.那IE和google浏览器都能正常解析了.

不用再去写<mvc:annotation-driven />,至于<mvc:annotation-driven />在背后做了什么,可看参考手册.注意spring版本哦,这两类从3.1才开始有的.

基于xml配置:

  1. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
  2. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
  3. <property name="messageConverters">
  4. <list>
  5. <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  6. <property name="supportedMediaTypes">
  7. <list>
  8. <value>text/html;charset=UTF-8</value>
  9. <value>application/json;charset=UTF-8</value>
  10. </list>
  11. </property>
  12. </bean>
  13. </list>
  14. </property>
  15. </bean>

基于Java-config方式会更加简单一点,先让你的mvc配置继承WebMvcConfigurerAdapter,再重写configureMessageConverters方法,加入jackson包,在controller使用@ResponseBody注解,OK!

如果要全局支持jsonp(支持jsonp的做法:可以在controller的方法返回String类型,接收一下callback,然后callback调用一下json结果就可以),可以再加一个StringHttpMessageConverter,不仅能解决中文乱码,还能把json里面的换行\r\n去掉.

  1. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
  2. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
  3. <property name="messageConverters">
  4. <list>
  5. <bean class="org.springframework.http.converter.StringHttpMessageConverter">
  6. <property name="supportedMediaTypes">
  7. <list>
  8. <value>text/html; charset=UTF-8</value>
  9. <value>application/json;charset=UTF-8</value>
  10. </list>
  11. </property>
  12. </bean>
  13. <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  14. <property name="supportedMediaTypes">
  15. <list>
  16. <value>text/html; charset=UTF-8</value>
  17. <value>application/json;charset=UTF-8</value>
  18. </list>
  19. </property>
  20. </bean>
  21. </list>
  22. </property>
  23. </bean>
 
 

spring-mvc注解(mvc:annotation-driver,JSON,配置详解)的更多相关文章

  1. Http请求中Content-Type讲解以及在Spring MVC注解中produce和consumes配置详解

    原文地址:  https://blog.csdn.net/shinebar/article/details/54408020 引言: 在Http请求中,我们每天都在使用Content-type来指定不 ...

  2. spring基于通用Dao的多数据源配置详解【ds1】

    spring基于通用Dao的多数据源配置详解 有时候在一个项目中会连接多个数据库,需要在spring中配置多个数据源,最近就遇到了这个问题,由于我的项目之前是基于通用Dao的,配置的时候问题不断,这种 ...

  3. Spring MVC配置文件的三个常用配置详解

    转自:http://www.cnblogs.com/benwu/articles/5162614.html Spring MVC项目中通常会有二个配置文件,sprng-servlet.xml和appl ...

  4. SpringBoot + Spring Security 基本使用及个性化登录配置详解

    Spring Security 基本介绍 这里就不对Spring Security进行过多的介绍了,具体的可以参考官方文档 我就只说下SpringSecurity核心功能: 认证(你是谁) 授权(你能 ...

  5. spring框架中AOP思想与各种配置详解

    Spring中提供两种AOP支持:   1.基于代理的经典AOP   2.Aspectj注解配置AOP    首先我们先了解什么是AOP,AOP(Aspect Oriented Programming ...

  6. 转载 Spring、Spring MVC、MyBatis整合文件配置详解

    Spring.Spring MVC.MyBatis整合文件配置详解   使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...

  7. 2017.3.31 spring mvc教程(二)核心流程及配置详解

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

  8. Spring学习 6- Spring MVC (Spring MVC原理及配置详解)

    百度的面试官问:Web容器,Servlet容器,SpringMVC容器的区别: 我还写了个文章,说明web容器与servlet容器的联系,参考:servlet单实例多线程模式 这个文章有web容器与s ...

  9. Spring MVC、MyBatis整合文件配置详解

    Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...

随机推荐

  1. Using View and Data API with Meteor

    By Daniel Du I have been studying Meteor these days, and find that Meteor is really a mind-blowing f ...

  2. 使用Fiddler抓取手机请求

    使用Fiddler抓取手机请求 Fiddler 手机 今天想尝试在手机上抓包,发现一个好玩的小工具——Fiddler. Fiddler是一个专门的抓包工具,可以模拟请求,修改请求,手机应用调试等.还是 ...

  3. 看看C# 6.0中那些语法糖都干了些什么(终结篇)

    终于写到终结篇了,整个人像在梦游一样,说完这一篇我得继续写我的js系列啦. 一:带索引的对象初始化器 还是按照江湖老规矩,先扒开看看到底是个什么玩意. 1 static void Main(strin ...

  4. freeswitch对接其它SIP设备

    这几天用到freeswitch对接其它设备方面的知识,这里整理下,也方便我以后查阅. 操作系统:debian8.5_x64 freeswitch 版本 : 1.6.8 一.freeswitch作为被叫 ...

  5. shell 脚本之 shell 练习题汇总

    整理了一些 shell 相关的练习题,记录到这里. 1. 请按照这样的日期格式 xxxx-xx-xx 每日生成一个文件,例如:今天生成的文件为 2013-09-23.log, 并且把磁盘的使用情况写到 ...

  6. 【推荐】CentOS安装Subversion-1.8.11+HTTP协议支持配置

    注:以下所有操作均在CentOS 6.5 x86_64位系统下完成. 我们需要搭建一个自己的SVN服务器. 此外,搭建好的SVN服务器除了需要支持svn协议外,最好还需要支持HTTP协议和HTTPS协 ...

  7. 阿里云ECS服务器配置(Ubuntu+JAVA+Tomcat+Mysql)

    最近购买了阿里云的ECS服务器,就服务器的安装配置做简要的说明,也方便日后查看. 1.远程操作服务器 远程操作服务器可以使用putty工具,下载地址:http://pan.baidu.com/s/1q ...

  8. Ubuntu Server 设置PPTP客户端连接

    安装PPTP客户端 apt-get install pptp-linux 设置连接账号信息 sudo vim /etc/ppp/chap-secrets 其中$login_name是登录名:$pass ...

  9. [笔记]ng2的webpack配置

    欢迎吐槽 前言 angular.cn教程中用的是systemjs加载器,那用webpack应该怎么配置呢?本文 demo: https://github.com/LeventZheng/angular ...

  10. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...