no declaration can be found for element 'mvc:annotation-driven'

user2164016 picture user2164016 · Mar 14, 2013 · Viewed 43.3k times · Source

I have the requirement of returning JSON/XML data from my controller.From what i found,I needed @ResponseBody in my method and for that I need <mvc:annotation-driven> enabled. I have tried all sorts of RnD but am still stuck! :(

Apparently my problem lies in my servlet.xml file (the schema isnt getting validated!) I am using Spring 3.1.1 & have explicitly put in spring-mvc-3.1.1.jar in my classpath.

Here's my servlet-context file sample-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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/tx                      http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/mvc-3.1  http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
         http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd">


  <context:component-scan base-package="com.sample.controller"/>
  <mvc:annotation-driven/>  

  <!--Use JAXB OXM marshaller to marshall/unmarshall following class-->
  <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />

  <bean id="xmlViewer" 
        class="org.springframework.web.servlet.view.xml.MarshallingView">
    <constructor-arg>
      <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
          <list>
            <value>com.sample.model.SampleClass</value>
          </list>
        </property>
      </bean>
    </constructor-arg>
  </bean>

  <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">

  <property name="viewResolvers">
    <list>
      <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
      </bean>
    </list>
  </property>
</bean> 

My controller class looks like this :

@Controller
public class XmlController {

  @RequestMapping(value="/getXml",method = RequestMethod.POST)
  public @ResponseBody AssociateDetail getXml(){
    System.out.println("inside xml controller.....");
    AssociateDetail assoBean=null;

    try{
      AssociateService add=new AssociateService();
      assoBean=add.selectAssociateBean();
    }catch(Exception e){
      e.printStackTrace();
    }

    return assoBean;    
  }
}

Now the problem is <mvc:annotation-driven /> gives error:

cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'.

And I have tried all the workarounds suggested on this site and beyond. Have updated my schema namespaces, using Spring 3.1.1 and @ResponseBody.

Answer

Usha picture Usha · Mar 14, 2013

As the error suggests there's something wrong with the schema declaration. You do not have the xsd s declared. Use this instead.

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    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">