Creative ideas have to be shared, copied and adapted!
Day quote…
  • If you can’t explain it simply, you don’t understand it well enough Albert Einstein

Solving “The matching wildcard is strict, but no declaration can be found for element mvc:annotation-driven”

java

If you are using Spring to create an awesome enterprise application and you are a beginner like me this article is for you. Today when I was integrating new configurations to Spring MVC to an existing project an error appeared, this error was related with <mvc-anotation-driven /> annotation. At the beginning I thought it would be easy to solve it, but not at all… so this is proof that in Enterprise Web Development, every single character in code is really important. The error was appearing is the next (also, see the image):

mvc-annotation-driven-error

The file with the problem (dispatcher-servlet.xml)

Solution

After spending more than three hours to try to fix this compile error,  the only wrong thing that I had was:

1) Version of XML schemes:

They should be compatible with:

2) Be sure, what Spring version you are using. In my case my .jar files are Spring 3.1 which is different to Spring 3.0 (this is very important).

3) Of course, you should check if you have Spring MVC jar file loaded in your project libraries.

So, the fixed file is this:

What does “<mvc-annotation-driven />” do?

Taken from the official documentation: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

“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:

  • Spring 3 style type conversion through a ConversionService instance in addition to the JavaBeans PropertyEditors used for Data Binding.
  • Support for formatting Number fields using the @NumberFormat annotation through the ConversionService.
  • Support for formatting Date, Calendar, Long, and Joda Time fields using the @DateTimeFormat annotation.
  • Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is present on the classpath.
  • 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.

That’s it!

Be happy with your code!

Comments

Alex Arriaga