spring-mvc注解(mvc:annotation-driver,JSON,配置详解)
一.DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter 的使用已经过时!
spring 3.1 开始我们应该用
RequestMappingHandlerMapping 来替换 DefaultAnnotationHandlerMapping,
用 RequestMappingHandlerAdapter 来替换 AnnotationMethodHandlerAdapter。
二.annotation-deiver详解
<mvc:annotation-driven /> 会自动注册RequestMappingHandlerMapping、RequestMappingHandlerAdapter 与xceptionHandlerExceptionResolver 三个bean。
还将提供以下支持:
- 支持使用 ConversionService 实例对表单参数进行类型转换;
- 支持使用 @NumberFormat annotation、@DateTimeFormat;
- 注解完成数据类型的格式化;
- 支持使用 @Valid 注解对 JavaBean 实例进行 JSR 303 验证;
- 支持使用 @RequestBody 和 @ResponseBody 注解;
- 当使用mvc:view-controller标签时一定要加入mvc:annotation-driven,不然会使requestMapping失效。
- 当为了处理静态资源问题而加入mvc:default-servlet-handler时,也一定要加入mvc:annotation-driven,不然requestMapping同样会失效。
- 当使用自定义类型转换器的时候需要加上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配置:
- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
- <property name="messageConverters">
- <list>
- <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
- <property name="supportedMediaTypes">
- <list>
- <value>text/html;charset=UTF-8</value>
- <value>application/json;charset=UTF-8</value>
- </list>
- </property>
- </bean>
- </list>
- </property>
- </bean>
基于Java-config方式会更加简单一点,先让你的mvc配置继承WebMvcConfigurerAdapter,再重写configureMessageConverters方法,加入jackson包,在controller使用@ResponseBody注解,OK!
如果要全局支持jsonp(支持jsonp的做法:可以在controller的方法返回String类型,接收一下callback,然后callback调用一下json结果就可以),可以再加一个StringHttpMessageConverter,不仅能解决中文乱码,还能把json里面的换行\r\n去掉.
- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
- <property name="messageConverters">
- <list>
- <bean class="org.springframework.http.converter.StringHttpMessageConverter">
- <property name="supportedMediaTypes">
- <list>
- <value>text/html; charset=UTF-8</value>
- <value>application/json;charset=UTF-8</value>
- </list>
- </property>
- </bean>
- <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
- <property name="supportedMediaTypes">
- <list>
- <value>text/html; charset=UTF-8</value>
- <value>application/json;charset=UTF-8</value>
- </list>
- </property>
- </bean>
- </list>
- </property>
- </bean>
spring-mvc注解(mvc:annotation-driver,JSON,配置详解)的更多相关文章
- Http请求中Content-Type讲解以及在Spring MVC注解中produce和consumes配置详解
原文地址: https://blog.csdn.net/shinebar/article/details/54408020 引言: 在Http请求中,我们每天都在使用Content-type来指定不 ...
- spring基于通用Dao的多数据源配置详解【ds1】
spring基于通用Dao的多数据源配置详解 有时候在一个项目中会连接多个数据库,需要在spring中配置多个数据源,最近就遇到了这个问题,由于我的项目之前是基于通用Dao的,配置的时候问题不断,这种 ...
- Spring MVC配置文件的三个常用配置详解
转自:http://www.cnblogs.com/benwu/articles/5162614.html Spring MVC项目中通常会有二个配置文件,sprng-servlet.xml和appl ...
- SpringBoot + Spring Security 基本使用及个性化登录配置详解
Spring Security 基本介绍 这里就不对Spring Security进行过多的介绍了,具体的可以参考官方文档 我就只说下SpringSecurity核心功能: 认证(你是谁) 授权(你能 ...
- spring框架中AOP思想与各种配置详解
Spring中提供两种AOP支持: 1.基于代理的经典AOP 2.Aspectj注解配置AOP 首先我们先了解什么是AOP,AOP(Aspect Oriented Programming ...
- 转载 Spring、Spring MVC、MyBatis整合文件配置详解
Spring.Spring MVC.MyBatis整合文件配置详解 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...
- 2017.3.31 spring mvc教程(二)核心流程及配置详解
学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...
- Spring学习 6- Spring MVC (Spring MVC原理及配置详解)
百度的面试官问:Web容器,Servlet容器,SpringMVC容器的区别: 我还写了个文章,说明web容器与servlet容器的联系,参考:servlet单实例多线程模式 这个文章有web容器与s ...
- Spring MVC、MyBatis整合文件配置详解
Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...
随机推荐
- [Android]使用Dagger 2依赖注入 - 图表创建的性能(翻译)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5098943.html 使用Dagger 2依赖注入 - 图表创 ...
- (五)什么是RDD-Java&Python版Spark
什么是RDD 视频教程: 1.优酷 2.YouTube RDD是个抽象类,全称为Resilient Distributed Datasets,是一个容错的.并行的数据结构,可以让用户显式地将数据存储到 ...
- 转载 c# 颜色对照表
这篇文章来来源于C# Color Table,这里是我翻译的中文版本,其中已经加上了我的一些理解和注释.翻译这篇文章的原因是我在写C#程序的时候发现,C#自带的颜色种类极多(详见下表),如果没有直观的 ...
- 数据结构(c语言)之学生信息管理系统
程序思维导图 代码表示(代码参考:长春大学-牛言涛老师) 如有错误请指出欢迎交流 #include<stdio.h> #include<malloc.h>//动态存储分配函数头 ...
- MDK st-link下载STM32程序出现Internal command error和Error:Flash download failed. Target DLL
MDK st-link下载STM32程序出现Internal command error和Error:Flash download failed. Target DLL 是因为目标板的芯片处于休眠 ...
- Backtracking algorithm: rat in maze
Sept. 10, 2015 Study again the back tracking algorithm using recursive solution, rat in maze, a clas ...
- Selenium-java-testng插件安装eclipse
1 进入Help----Install 2 点击 ADD 输入: TestNG http://beust.com/eclipse 点击OK 3 等一会会加载出如下图,勾选,点击Next 下一步 4 ...
- Linux线程基础
复习中掌握线程的基本管理即可,而不用考虑线程的同步: 创建线程花费的代价,比创建进程小得多,所以同一个进程的,多个线程执行多个任务-->比多个进程执行多个任务更有效率. 线程也分为用户级线程.内 ...
- [LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- [LeetCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...