Custom Type Converter in Spring MVC

Updated on August 1st, 2019

In this post of Spring MVC, we will look creating a custom type converter in Spring MVC. By default, Spring only knows how to convert simple types. This means if we pass simple type in our request, Spring MVC type converter will automatically bind them to correct Java types.



In the real world application, we may come across use cases when we need to bind complex object as part of the request data. In this post, we will create a custom type converter in Spring MVC that can convert the request parameter into Java 8 LocalDate and LocalDateTime objects.


1. Creating a Custom Type Converter in Spring MVC

To create a custom type converter, we need to implement the Converter<S,T> interface.While implementing this interface we need to pass following 2 parameters to the method.

  • S – The source object type.
  • T –  The Target object type 

In this post, we will create two custom converter for LocaleDate and LocaleDateTime. We will use Spring Boot for this post as it provides an automatic registration of Converter beans but the core logic will remain same if you are working on a simple Spring MVC application.


2. Maven Setup

We will create a simple Spring Boot based web application. You can create the application either through the IDE or can use Spring Initializr. This is how our pom.xml look like:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" 
    xmlns:xsi="" xsi:schemaLocation="">
        <!-- lookup parent from repository -->
    <description>How to create custom type converter in Soring MVC</description>






3. Creating Custom LocalDateConverter

To create the custom converter, we will create a custom class LocalDateConverter and implement the Convert  method in the Converter interface.

package com.javadevjournal.converter;

import org.springframework.core.convert.converter.Converter;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateConverter implements Converter<String, LocalDate> {

     * Override the convert method
     * @param date
     * @return
    public LocalDate convert(String date) {
        return LocalDate.parse(date, DateTimeFormatter.BASIC_ISO_DATE);

You may have to add null check if working with Spring version 4.0 or the older one. We have annotated our class with @Component annotation.Spring Boot will automatically register this converter. We don’t need any additional configurations to register the custom converter.


4. Testing Custom Converter

To test our custom converter, let’s create a simple REST controller to get order information based on the date. This is how our REST Controller look like:

package com.javadevjournal.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

public class OrderController {

    private static final Logger LOG = LoggerFactory.getLogger(OrderController.class);

    public Order getOrderByDate(@PathVariable("date") LocalDate date) {"Sending order for the request date {} ", date);
        return new Order(300.40, "1234", date);

Let’s build and deploy our application to see things in action:

Custom Type Converters With Spring MVC

We are passing date in BASIC_ISO_DATE format since we are using this formatter to parse the request data.If we change the format of the request, we will get an error back from the API.

Type Converters


5. Configuration Spring MVC Application

If you are not using Spring Boot, you may not use the auto-registration feature of the Spring Boot. We need to register the custom type converter in Spring MVC application. This is how we can register our custom converter:

public class WebConfig implements WebMvcConfigurer {
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new LocalDateConverter());



In this post, we saw how to create a custom type converter in Spring MVC. We look at creating custom converter in both Spring MVC as well Spring Boot based application. As always, the source code for this post is available on the GitHub.

0 0 vote
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x