All Downloads are FREE. Search and download functionalities are using the official Maven repository.

JavaJaxRS.resteasy.JodaDateTimeProvider.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
package {{apiPackage}};

import org.joda.time.DateTime;
import {{javaxPackage}}.ws.rs.ext.ParamConverter;
import {{javaxPackage}}.ws.rs.ext.ParamConverterProvider;
import {{javaxPackage}}.ws.rs.ext.Provider;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import {{javaxPackage}}.ws.rs.WebApplicationException;
import {{javaxPackage}}.ws.rs.core.Response;


@Provider
public class JodaDateTimeProvider implements ParamConverterProvider {

    public static class JodaDateTimeConverter implements ParamConverter {

        public DateTime fromString(String string) {
            try {
                DateTime dateTime = DateTime.parse(string);
                return dateTime;
            } catch (Exception e) {
                throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).
                entity(string + " must be valid DateTime").build());
            }
        }

        public String toString(DateTime t) {
            return t.toString();
        }
    }

    public  ParamConverter getConverter(Class type, Type type1, Annotation[] antns) {
        if (DateTime.class.equals(type)) {
            return (ParamConverter) new JodaDateTimeConverter();
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy