dropwizard-java8 icon indicating copy to clipboard operation
dropwizard-java8 copied to clipboard

Add support for Java8 date/time @QueryParams

Open cowwoc opened this issue 9 years ago • 4 comments

The bundle seems to suppose using date/times as path parameters, but you are missing an implementation for query parameters.

cowwoc avatar May 03 '16 16:05 cowwoc

@cowwoc Would moving to Dropwizard 1.0.0-rc2 (which supports the Java Date/Time API in JAX-RS parameters) be an option for you?

joschi avatar May 04 '16 08:05 joschi

@joschi I just tried upgrading and ran into a lot of problems (conflicting transitive dependencies, classloader issues, etc). I don't think the Dropwizard dependencies are fully caught up yet.

cowwoc avatar May 05 '16 17:05 cowwoc

Here is an implementation that we wrote that worked for us:

@Provider
@Singleton
public class LocalDateTimeConverter implements ParamConverterProvider {

    @Override public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {

        if (!rawType.isAssignableFrom(LocalDateTime.class)) {
            return null;
        }
        return new ParamConverter<T>() {
            @Override
            @SuppressWarnings("unchecked")
            public T fromString(final String value) {
                if (value == null) {
                    throw new IllegalArgumentException("value may not be null");
                }
                return (T) LocalDateTime.parse(value);
            }

            @Override
            public String toString(final T value) {
                return ((LocalDateTime) value).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
            }
        };
    }

}

UPDATED on November 16th 2016. Original implementation of fromString() did not handle null values correctly.

cowwoc avatar May 05 '16 17:05 cowwoc

I just tried upgrading and ran into a lot of problems (conflicting transitive dependencies, classloader issues, etc).

Did you post this to the Dropwizard mailing list or open an issue on GitHub?

I don't think the Dropwizard dependencies are fully caught up yet.

Could you please elaborate on this?

joschi avatar May 09 '16 11:05 joschi