Enable MVC Configuration

你可以使用@EnableWebMvc注释,通过以编程方式配置来启用MVC配置,或者使用带有XML配置的<mvc:annotation-driven>,如下例所示:

You can use the @EnableWebMvc annotation to enable MVC configuration with programmatic configuration, or <mvc:annotation-driven> with XML configuration, as the following example shows:

@Configuration
@EnableWebMvc
public class WebConfiguration {
}

当使用 Spring Boot 时,您可能想要使用类型为 WebMvcConfigurer@Configuration 类,但没有 @EnableWebMvc 来保留 Spring Boot MVC 自定项。请参阅 the MVC Config API section 中和 [专用 Spring Boot 文档](https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#web.servlet.spring-mvc.auto-configuration) 中的更多详细信息。

When using Spring Boot, you may want to use @Configuration classes of type WebMvcConfigurer but without @EnableWebMvc to keep Spring Boot MVC customizations. See more details in the MVC Config API section and in the dedicated Spring Boot documentation.

前面的示例注册了多个Spring MVC基础结构bean并适应类路径上可用的依赖项(例如,用于JSON、XML等有效负载转换器)。

The preceding example registers a number of Spring MVC infrastructure beans and adapts to dependencies available on the classpath (for example, payload converters for JSON, XML, and others).