但是使用RequestMappingHandlerAdapter后无法识别@RequestMapping注解
时间: 2023-07-26 09:08:01 浏览: 142
如果你使用了`RequestMappingHandlerAdapter`但是无法识别`@RequestMapping`注解,可能是因为你没有在配置类中添加`<mvc:annotation-driven/>`配置。这个配置会启用Spring MVC的注解驱动,包括`@RequestMapping`注解的识别和解析。你可以在Spring MVC的配置文件中添加如下配置:
```xml
<mvc:annotation-driven/>
```
或者如果你使用的是Java Config,可以在`WebMvcConfigurer`的实现类中添加如下方法:
```java
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
// ...
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
// ...
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// ...
}
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
// ...
}
@Override
public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
// ...
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// ...
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
// ...
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
// ...
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// ...
}
@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
// ...
}
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(false);
configurer.setUseTrailingSlashMatch(true);
}
@Override
public void addCorsMappings(CorsRegistry registry) {
// ...
}
@Override
public void configureViewControllers(ViewControllerRegistry registry) {
// ...
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// ...
}
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
// ...
}
@Override
public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
// ...
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// ...
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
// ...
}
@Override
public void addFormatters(FormatterRegistry registry) {
// ...
}
@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
// ...
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// ...
}
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(false);
configurer.setUseTrailingSlashMatch(true);
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// ...
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
// ...
}
@Override
public void addCorsMappings(CorsRegistry registry) {
// ...
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
// ...
}
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
// ...
}
@Override
public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
// ...
}
// Add this method to enable Spring MVC annotation-driven
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// ...
}
}
```
确保在`configureMessageConverters`方法中添加了`<mvc:annotation-driven/>`配置。这样就能够启用Spring MVC的注解驱动,使得`@RequestMapping`注解能够被正确识别和解析。
阅读全文