Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ParamConverterProvider implementations for Date/Time classes #96

Closed
cowwoc opened this issue Nov 15, 2016 · 3 comments
Closed

ParamConverterProvider implementations for Date/Time classes #96

cowwoc opened this issue Nov 15, 2016 · 3 comments

Comments

@cowwoc
Copy link

cowwoc commented Nov 15, 2016

(Copied from FasterXML/jackson-modules-java8#7)

Please provide ParamConverterProvider implementations for all date/time types that make sense to use as query parameters. For example, here is an implementation I used to pass LocalDateTime as a query parameter (feel free to use it):

/**
 * Enables LocalDateTime to be used as a QueryParam.
 *
 * @author Gili Tzabari
 */
@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);
            }
        };
    }
}
@cowtowncoder
Copy link
Member

Also just realized that there are some subtleties wrt where to locate this (project-wise), due to dependency to Java 8.

But one thing I am wondering is this: since nothing in above relies on Jackson, does this even make sense here? Ideally various configuration settings would be used, but unless there is some way to access ObjectMapper or ObjectReader, that can't be done. So in a way maybe this would belong more to DropWizard project?

@cowwoc
Copy link
Author

cowwoc commented Nov 16, 2016

Good point, and in retrospect it seems that I caught this in the past but forgot about it :) dropwizard/dropwizard-java8#26

Closing this issue.

@cowwoc cowwoc closed this as completed Nov 16, 2016
@cowtowncoder
Copy link
Member

@cowwoc ok no problem. Took a while for me to figure it out too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants