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

Description Resource Path Location Type
cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'. dispatcher-servlet.xml dealerweb/WebContent/WEB-INF line 28 XML Problem

cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:resources'. dispatcher-servlet.xml dealerweb/WebContent/WEB-INF line 27 XML Problem

mvc-annotation-driven-error

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"  
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

	<context:property-placeholder location="classpath:/config.properties"/>

	<!-- Spring will search in the bellow paths controller an services annotations-->	
	<context:component-scan base-package="com.rest.web" /> 
 	<mvc:resources mapping="/css/**" location="/css/" />
	<mvc:annotation-driven />

	<!-- More code goes here -->
</beans>

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:

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context/spring-context-3.0.xsd

They should be compatible with:

http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

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:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"  
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

	<context:property-placeholder location="classpath:/config.properties"/>

	<!-- Spring will search in the bellow paths controller an services annotations-->	
	<context:component-scan base-package="com.rest.web" /> 
 	<mvc:resources mapping="/css/**" location="/css/" />
	<mvc:annotation-driven />

	<!-- More code goes here -->
</beans>

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!

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

Leave a Reply

Your email address will not be published. Required fields are marked *