What does <mvc: annotation-driven / "> do?
I use file names in my REST API (example: GET http: //xxx/api/myImage.jpg ) @PathVariable problem dropped ".jpg". these problems have already been asked several times here and answered. but did not work with me.
so I searched, then found in
https://jira.springsource.org/browse/SPR-6524
"... itβs just not supposed to be combined with manual instances of DefaultAnnotationHandlerMapping, which are currently designed as either either - or a choice very similar to and."
"mvc namespace simplifies configuration."
The real question is mvc, what should I do? and changed?
I found this myself.
- reconfigured firewall. (mvc namspace is required in a bean configuration)
- useDefaultSuffixPattern does not work.
- Adds a JSON message converter. if the jackson library is available.
- @PathVariable arguments are automatically added to the model
Any others?
Thanks in advance!
The mvc:annotationDriven tag essentially sets your Spring context for you to send requests to controllers.
The tag will configure two beans DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter.
You can find more information in the Spring docs:
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html
Before I provided specific points, let me clarify the answer provided by Roy is not accurate. You do not need to specify the mvc:annotation-driven tag to create a default instance of beans. This tag can be used by Spring 3.0+ to enable a new feature from Spring 3.0
( Do not use it if backward compatibility is required, especially if you are using old controller-based classes such as MultiActionController , SimpleFormController )
Now let's see what this tag actually does -
Before Spring 3.1, by default beans, where
- DefaultAnnotationHandlerMapping
- AnnotationMethodHandlerAdapter
- AnnotationMethodHandlerExceptionResolver
They are deprecated in Spring 3.1, and if you use the above tag, it will be replaced by <
- RequestMappingHandlerMapping
- RequestMappingHandlerAdapter
- ExceptionHandlerExceptionResolver
DefaultAnnotationHandlerMapping decided which controller to use, and AnnotationMethodHandlerAdapter chose the actual request processing method. RequestMappingHandlerMapping performs both tasks. Therefore, the request is directly mapped to the method.
There is another beans infrastructure that is created using these tags ( attached in addition to the default values ), for example, MappedInterceptor , ConfigurableWebBindingInitializer , SessionFlashManager , ContentNegociationManager , etc. I am not going to explain this :), because each of them answers for a long time, so google for more information.
PS: And yes, Spring 3.1+ automatically exposes @PathVariables for the model. You also have the mvc:interceptors tag. But I think this is not related to <mvc:annotation-driven /> . I would highly recommend reading - http://spring.io/blog/2009/12/21/mvc-simplifications-in-spring-3-0/
To enable Java MVC configuration, add the @EnableWebMvc annotation to one of the @Configuration classes:
@Configuration @EnableWebMvc public class WebConfig { } To achieve the same in XML, use the <Strong> MVC: drive annotation element in the DispatcherServlet context (or in the root context if you do not have a DispatcherServlet context):
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven/> </beans> The above registers RequestMappingHandlerMapping , RequestMappingHandlerAdapter and ExceptionHandlerExceptionResolver (among others) support processing requests using methods of annotated controllers using annotations such as @RequestMapping, @ExceptionHandler and others.
For more details see the link below:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config
mvc: annotation-linked tag does extra work out of context: component scan tag
The tag registers the handler - adapter mapping binding needed to send requests to your @Controllers:
Taghelps register the following components.
DefaultAnnotationHandlerMapping . This is an implementation of HandlerMapping that maps HTTP requests to handler methods defined using the @RequestMapping annotation.
AnnotationMethodHandlerAdapter . He is responsible for scanning the controllers to determine the methods (and parameters) annotated with @MVC annotations. It scans and processes handler methods annotated using @RequestMapping. Also handles annotations @RequestParam, @ModelAttribute, @SessionAttributes and @InitBinder.
ConfigurableWebBindingInitializer . Initializer for linking web data. Helps in declarative configuration of Web Binder using validators, transformation services, property editors, etc.
LocalValidatorFactoryBean - implements the validator interface and enables JSR303 validation. This is introduced in ConfigurableWebBindingInitializer. FormattingConversionServiceFactoryBean is a factory conversion that returns conversion services for basic objects such as date and numbers. This factory is reintroduced into ConfigurableWebBindingInitializer.
Message Converters
ByteArrayHttpMessageConverter . An HTTP request message converter that reads the body of an HTTP message and returns a stream of bytes. It can also read a stream of bytes and build a response body. Used to receive and send documents such as PDF, XLS, etc.
StringHttpMessageConverter . An HTTP request message converter that reads the body of a plain text request and associates it with a String object. And vice versa with the answer.
FormHttpMessageConverter . An HTTP request message converter that reads the body of a request encoded in a form and associates it with a Binding object. SourceHttpMessageConverter is an HTTP request converter that converts the body of an XML message to / from a binding object.
If we do not use the mvc: annotation tag, then we must manually register these components manually in the XML file in order to use them, which leads to too much additional work.