Generic Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:


1. Overview

In this quick tutorial, we'll take a look at what it takes to create a simple Spring MVC project with the Kotlin language.

2. Maven

For the Maven configuration, we need to add the following Kotlin dependencies:


We also need to add the following Spring dependencies:


To compile our code, we need to specify our source directory and configure the Kotlin Maven Plugin in the build section of our pom.xml:


3. Spring MVC Configuration

We can use either the Kotlin annotation configuration or an XML configuration.

3.1. Kotlin Configuration

The annotation configuration is pretty simple. We setup view controllers, the template resolver, and the template engine. Thereafter we can use them to configure the view resolver:

open class ApplicationWebConfig : WebMvcConfigurerAdapter(), 
  ApplicationContextAware {

    private var applicationContext: ApplicationContext? = null

    override fun setApplicationContext(applicationContext: 
      ApplicationContext?) {
        this.applicationContext = applicationContext

    override fun addViewControllers(registry:
      ViewControllerRegistry?) {

    open fun templateResolver(): SpringResourceTemplateResolver {
        return SpringResourceTemplateResolver()
          .apply { prefix = "/WEB-INF/view/" }
          .apply { suffix = ".html"}
          .apply { templateMode = TemplateMode.HTML }
          .apply { setApplicationContext(applicationContext) }

    open fun templateEngine(): SpringTemplateEngine {
        return SpringTemplateEngine()
          .apply { setTemplateResolver(templateResolver()) }

    open fun viewResolver(): ThymeleafViewResolver {
        return ThymeleafViewResolver()
          .apply { templateEngine = templateEngine() }
          .apply { order = 1 }

Next, let's create a ServletInitializer class. The class should extend AbstractAnnotationConfigDispatcherServletInitializer. This is a replacement for the traditional web.xml configuration:

class ApplicationWebInitializer: 
  AbstractAnnotationConfigDispatcherServletInitializer() {

    override fun getRootConfigClasses(): Array<Class<*>>? {
        return null

    override fun getServletMappings(): Array<String> {
        return arrayOf("/")

    override fun getServletConfigClasses(): Array<Class<*>> {
        return arrayOf(

3.2. XML Configuration

The XML equivalent for the ApplicationWebConfig class is:

<beans xmlns="...">
    <context:component-scan base-package="com.baeldung.kotlin.mvc" />

    <mvc:view-controller path="/welcome.html"/>

    <mvc:annotation-driven />

    <bean id="templateResolver" 
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML" />

    <bean id="templateEngine"
        <property name="templateResolver" ref="templateResolver" />

    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
        <property name="order" value="1" />


In this case, we do have to specify the web.xml configuration as well:

<web-app xmlns=...>

    <display-name>Spring Kotlin MVC Application</display-name>



4. The Html Views

The corresponding html resource is located under the /WEB-INF/view directory. In the above view controller configuration, we defined a basic view controller, welcome.html. The content of the corresponding resource is:


        <h1>Body of the welcome view</h1>

5. Conclusion

After running the project, we can access the configured welcome page at http://localhost:8080/welcome.html.

In this article, we configured a simple Spring MVC project using both a Kotlin and XML configuration.

The complete source code is available over on GitHub.

Generic bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

Newest Most Voted
Inline Feedbacks
View all comments
3 years ago

It should be mentioned that using a compiler plugin it is not necessary to have the ApplicationWebConfig class “open”

Grzegorz Piwowarek
3 years ago
Reply to  KPNC

There is another article on the way about this 🙂

Comments are closed on this article!