springmvc配置之mvc:annotation-driven
为了简化springmvc配置,spring同时引入了mvc namespace, 配置了
<mvc:annotation-driven/>
spring会默认注册a RequestMappingHandlerMapping, a RequestMappingHandlerAdapter, and an ExceptionHandlerExceptionResolver 三个bean, 来支持使用注解(@RequestMapping、@ExceptionHandler、@Controller等)修饰的controller处理请求。同时还有以下一些特性:
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:
a. ByteArrayHttpMessageConverter converts byte arrays.
b. StringHttpMessageConverter converts strings.
c. ResourceHttpMessageConverter converts to/from org.springframework.core.io.Resource for all media types.
d. SourceHttpMessageConverter converts to/from a javax.xml.transform.Source.
e. FormHttpMessageConverter converts form data to/from a MultiValueMap<String, String>.
f. Jaxb2RootElementHttpMessageConverter converts Java objects to/from XML — added if JAXB2 is present and Jackson 2 XML extension is not present on the classpath.
g. MappingJackson2HttpMessageConverter converts to/from JSON — added if Jackson 2 is present on the classpath.
h. MappingJackson2XmlHttpMessageConverter converts to/from XML — added if Jackson 2 XML extension is present on the classpath.
i. AtomFeedHttpMessageConverter converts Atom feeds — added if Rome is present on the classpath.
j. RssChannelHttpMessageConverter converts RSS feeds — added if Rome is present on the classpath.
配置示例:
<bean name="fastJson" class="com.alibaba.fastjson.support.config.FastJsonConfig">
<property name="features">
<list>
<value>AllowSingleQuotes</value>
</list>
</property>
<property name="serializerFeatures">
<list>
<value>QuoteFieldNames</value>
<value>WriteEnumUsingToString</value>
<value>WriteEnumUsingName</value>
<value>WriteNullNumberAsZero</value>
<value>SkipTransientField</value>
<value>WriteDateUseDateFormat</value>
</list>
</property>
</bean>
<!--默认的mvc注解映射的支持 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean 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>
<value>text/json;charset=UTF-8</value>
</list>
</property>
<property name="fastJsonConfig" ref="fastJson"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
引用:
1、 springdoc
2、what's annotation-driven do
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
2013-05-06 LeetCode_Merge k Sorted Lists