Configure Spring WebMVC
Firstly add Spring WebMvc related dependenices into pom.xml.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>To bootstrap a Spring MVC application, you have to enable Spring built-in DisptachServlet.
Serlvet 3.0 provides a new feature ServletInitializer to configure web applciation without web.xml.
Spring has its WebApplicationInitializer interface, there are a few classes implement this interface, AbstractAnnotationConfigDispatcherServletInitializer includes configuration of Spring Dispatch Servlet, and leaves some room to customize DispatchServlet.
Declare a AbstractAnnotationConfigDispatcherServletInitializer bean.
@Order(0)
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {
AppConfig.class, //
DataSourceConfig.class, //
JpaConfig.class, //
DataJpaConfig.class,//
SecurityConfig.class,//
Jackson2ObjectMapperConfig.class,//
MessageSourceConfig.class
};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {
WebConfig.class, //
SwaggerConfig.class //
};
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
encodingFilter.setEncoding("UTF-8");
encodingFilter.setForceEncoding(true);
return new Filter[] { encodingFilter };
}
}getRootConfigClasses specifies the configuration classes should be loaded for the Spring infrastrucuture.
getServletConfigClasses specifies the configurations depend on Servlet specification, esp, web mvc related configurations.
getServletMappings is the Spring DispatchServlet mapping URL pattern.
getServletFilters are those Web Filters will be applied on the Spring DispatchServlet.
Spring MVC DispatchServlet is configured in the super classes, explore the details if you are interested in it.
In getServletConfigClasses, we specify a WebConfig will be loaded, which is responsible for configuring Spring MVC in details, including resource handling, view, view resolvers etc.
A classic WebConfig looks like.
Generally, WebMvcConfigurerAdapter is the extension point left for users to use for customizing MVC configurations.
Spring Data project provides a SpringDataWebConfiguration which is a subclass of WebMvcConfigurerAdapter and adds pagination, sort, and domain object conversion support. Open the source code of SpringDataWebConfiguration and research yourself.
The following is the full source code of WebConfig.
@EnableWebMvctells Spring to enable Spring MVC.@ComponentScanuses a filter to select Spring MVC related classes to be activated.addResourceHandlersconfigure how to handle static resources.addViewControllersleave places to configure view resolver to render specific views.configureHandlerExceptionResolversspecifies exception handling stretagy.configureMessageConvertersconfigureHttpMessageConverterwill be used for serialization and deserialization.
I would like use application/json as default content type, and uses Jackson to serialize and deserialize messages.
Configure a Jackson ObjectMapper as you need.
Jackson2ObjectMapperBuilder provides fluent APIs to configure which features will be enabled or disabled for serialization and deserialization. For the complete configurable options, check the javadocs of SerializationFeature and DeserializationFeature.
Last updated
Was this helpful?