Configuring DateFormat with Spring Boot and RestEasy

This is so finely tuned, I pity the fool who has to make it work (me).

A couple of things to check. First, ensure you Application (with main method) is in a package a level above components to be autowired, especially the @Producer. Also, ensure it has these annotations:

@ComponentScan
@SpringBootApplication

Second, create a configuration type which is both a @Provider and a @Component.

Ensure that class imports of configuration classes such as ObjectMap are only from the package com.fasterxml.jackson. Don’t import anything from Jackson1, org.codehaus.jackson. Don’t mix Jackson1 and 2 and keep 1 out of the implementation.

I’m including the imports so there is no ambiguity about this.

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.joda.JodaMapper;

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

@Provider
@Component
public class DateObjectMapperProvider implements ContextResolver<ObjectMapper> {
    private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public ObjectMapper getContext(Class<?> type) {
        dateFormat.setLenient(false);
        dateFormat.setTimeZone(TimeZone.getTimeZone("PST"));
        
        final ObjectMapper result = new JodaMapper();
        result.setDateFormat(dateFormat);
        return result;
    }

}

Maven dependencies, including Swagger. I have not included Spring.


<swagger.version>1.3.12</swagger.version>
<resteasy.version>3.0.9.Final</resteasy.version>
...
                         <dependency>
				<groupId>org.jboss.resteasy</groupId>
				<artifactId>resteasy-spring</artifactId>
				<version>${resteasy.version}</version>
			</dependency>
			<dependency>
				<groupId>org.jboss.resteasy</groupId>
				<artifactId>resteasy-jackson2-provider</artifactId>
				<version>${resteasy.version}</version>
			</dependency>
			<dependency>
				<groupId>org.jboss.resteasy</groupId>
				<artifactId>resteasy-validator-provider-11</artifactId>
				<version>${resteasy.version}</version>
			</dependency>
			<dependency>
				<groupId>org.jboss.resteasy</groupId>
				<artifactId>resteasy-multipart-provider</artifactId>
				<version>${resteasy.version}</version>
			</dependency>
			<dependency>
				<groupId>org.jboss.resteasy</groupId>
				<artifactId>resteasy-jaxb-provider</artifactId>
				<version>${resteasy.version}</version>
			</dependency>

			<dependency>
				<groupId>com.wordnik</groupId>
				<artifactId>swagger-jaxrs_2.10</artifactId>
				<version>${swagger.version}</version>
			</dependency>
			<dependency>
				<groupId>com.wordnik</groupId>
				<artifactId>swagger-core_2.10</artifactId>
				<version>${swagger.version}</version>
			</dependency>
			
			<dependency>
				<groupId>com.mangofactory</groupId>
				<artifactId>swagger-springmvc</artifactId>
				<version>1.0.2</version>
			</dependency>
	
			<dependency>
				<groupId>org.ajar</groupId>
				<artifactId>swagger-spring-mvc-ui</artifactId>
				<version>0.4</version>
			</dependency>			

		<dependency>
		<groupId>com.fasterxml.jackson.datatype</groupId>
			<artifactId>jackson-datatype-joda</artifactId>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
		</dependency>

Set it up this way and you should be golden.