According to the jersey documentation, for a class to be supported as a @QueryParam
, it needs to meet the following requirements:
- To be a primitive kind;
- Has a builder that accepts only one
String
as an argument;
- Have a static method named
fromString
or valueOf
and accept only one String
as an argument;
- Have an interface implementation
javax.ws.rs.ext.ParamConverterProvider
that returns an instance of the interfacejavax.ws.rs.ext.ParamConverter
, or;
- Be a
List<T>
, Set<T>
or SortedSet<T>
, where T
satisfy options 2 or 3.
The only criterion that class Date
satisfied was the 2, however, as the builder Date(String)
was marked as @Deprecated
(obsolete), Jersey developers opted for stop supporting him from the Jersey 2.x.
Therefore, the only option left is to implement a javax.ws.rs.ext.ParamConverterProvider
and register it. Example:
public class DateParamProvider implements ParamConverterProvider {
@SuppressWarnings("unchecked")
@Override
public <T> ParamConverter<T> getConverter(final Class<T> rawType, final Type genericType,
final Annotation[] annotations) {
if (rawType != Date.class) {
return null;
}
//TODO teste se esse é realmente o formato utilizado pelas suas datas
final DateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z");
return (ParamConverter<T>) new ParamConverter<Date>() {
@Override
public Date fromString(final String value) {
try {
return format.parse(value);
} catch (final ParseException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public String toString(final Date value) {
return format.format(value);
}
};
}
}
And register it (it will depend on how you set up your project. Ex: via javax.ws.rs.core.Application
, web.xml
or org.glassfish.jersey.server.ResourceConfig
):
javax.ws.rs.core.Application
//TODO ajuste para o caminho da sua aplicação
@ApplicationPath("/")
public class InitApp extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<>();
classes.add(SuaClasseAnotadoComPath.class); //Sua classe anotado com @Path
classes.add(DateParamProvider.class);
return classes;
}
}
org.glassfish.jersey.server.ResourceConfig
//TODO ajuste para o caminho da sua aplicação
@ApplicationPath("/")
public class InitApp extends ResourceConfig {
public InitApp() {
register(SuaClasseAnotadoComPath.class); //Sua classe anotado com @Path
register(DateParamProvider.class);
}
}
Another simpler solution would be to receive the parameter as a String
and, within the method itself, make the conversion to Date
.
Note: If there is no obligation to use the class Date
, take a look at new classes inserted in Java 8 of the package java.time
, in this particular case, in the class ZonedDateTime
(even with this class, the use of an implementation of a javax.ws.rs.ext.ParamConverterProvider
it is still necessary).
you want to send the current date?
– viana
I have two date inputs the user chooses the date he wants to filter. In the case there only has a date parameter that is filterBegin, because I’m only testing with a parameter first. ?
– Guilherme Nass
Take a look at this response from the gringos, it might help you: http://stackoverflow.com/a/9522619/2570426
– viana
Perhaps the problem is related to the date format.
– viana
I think the link you gave me wouldn’t help my situation. So the format I’m sending is Sun Oct 10 2010 01:00:00 GMT-0200
– Guilherme Nass
You will have to pass the date as a String and do the conversion in the back-end, ie, change your service to receive a String instead of a Date.
– Erick Maia
I suggest you abandon the
Date
and migrate to theLocalDate
,LocalDateTime
orOffsetDateTime
, depending on which is more appropriate. See this: http://answall.com/q/177129/132– Victor Stafusa