I am providing an implementation of ParamConverterProvider
in a JAX-RS application. This implementation provides gives a definition of the abstract method in the interface with signature as:
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation annotations[]);
I am playing with an online tutorial and modified the implementation as follows.
package org.koushik.javabrains.rest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Calendar;
import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Provider;
import org.koushik.javabrains.rest.MyDate;
@Provider
public class MyDateConverterProvider implements ParamConverterProvider {
@Override
public <MyDate> ParamConverter<MyDate> getConverter(final Class<MyDate> rawType, Type genericType, Annotation[] annotations) {
return new ParamConverter<MyDate>() {
@Override
public MyDate fromString(String value) {
Calendar requestedDate = Calendar.getInstance();
if ("tomorrow".equalsIgnoreCase(value)) {
requestedDate.add(Calendar.DATE, 1);
}
else if ("yesterday".equalsIgnoreCase(value)) {
requestedDate.add(Calendar.DATE, -1);
}
org.koushik.javabrains.rest.MyDate myDate = new org.koushik.javabrains.rest.MyDate();
myDate.setDate(requestedDate.get(Calendar.DATE));
myDate.setMonth(requestedDate.get(Calendar.MONTH));
myDate.setYear(requestedDate.get(Calendar.YEAR));
return rawType.cast(myDate);
}
@Override
public String toString(MyDate myBean) {
if (myBean == null) {
return null;
}
return myBean.toString();
}
};
}
}
I have a few questions:
- Why do i need to provide the entire package name when instatiatiating the type T that will be returned from
getConverter
. It has the same package name as this current class, still I need to write the fully qualified name , else I get a compilation error , cannot instantiate.org.koushik.javabrains.rest.MyDate myDate = new org.koushik.javabrains.rest.MyDate();
. It doesn't make a difference if I import this at the top.import org.koushik.javabrains.rest.MyDate;
- At the start of the method I get this warning The type parameter MyDate is hiding the type MyDate. This provider works fine and am able to cast request path params, but am still wondering how can I avoid the above two.