求助:Sts <mvc:annotation-drive/>报错,不知原因及解决方法
Hey there, let's break down the common issues that cause errors with <mvc:annotation-driven/> in Spring MVC—since you didn't share the exact error message or full code, I'll cover the most frequent culprits and fixes you can check:
<mvc:annotation-driven/> Errors 1. Missing or Mismatched Spring MVC Dependencies
If you're using Maven or Gradle, forgetting core Spring MVC artifacts or having mismatched Spring versions across dependencies is a top cause.
For Maven, ensure you have this dependency (replace your-spring-version with your actual version):
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>your-spring-version</version> </dependency>
For Gradle:
implementation 'org.springframework:spring-webmvc:your-spring-version'
Double-check that all Spring-related dependencies use the same version—version conflicts often break annotation processing silently.
2. Incorrect Namespace Declaration in XML
The <mvc:annotation-driven/> tag relies on the Spring MVC namespace. If your XML config is missing the namespace or correct schema location, the parser won't recognize the tag. Your root context should look like this:
<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/> <!-- Your other bean definitions --> </beans>
Make sure the schema URLs match your Spring version (older versions may have slightly different schema paths).
3. Conflict with Manual Handler Components
If you've manually defined RequestMappingHandlerMapping or RequestMappingHandlerAdapter beans alongside <mvc:annotation-driven/>, you'll hit conflicts. The <mvc:annotation-driven/> tag automatically registers these components with default settings.
To fix this:
- Either remove your manual bean definitions and customize behavior via
<mvc:annotation-driven/>attributes (likeconversion-serviceormessage-converters), or - Add
register-defaults="false"to<mvc:annotation-driven/>and fully configure your own handler beans.
4. JDK Version Incompatibility
Older Spring versions (pre-5.0) require JDK 1.6+, while Spring 5+ needs JDK 8 or higher. If your project uses a JDK version that doesn't align with your Spring version, annotation processing will fail. Verify your project's JDK level matches Spring's requirements.
5. Missing Annotation Processing or Converter JARs
If you're seeing errors related to JSON serialization, type conversion, or annotation processing, you might be missing supporting JARs. For example, to fix JSON-related issues, add the Jackson dependency:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>your-jackson-version</version> </dependency>
Next Steps for Precise Troubleshooting
If none of these resolve your issue, share:
- The full error stack trace
- Your complete Spring XML configuration
- Your pom.xml/gradle.build dependency list
- The exact JDK and Spring versions you're using
That will help us zero in on the exact problem!
内容的提问来源于stack exchange,提问作者YongGwan Lee

