0

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:

  1. 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;
  2. 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.
Rpant
  • 974
  • 2
  • 14
  • 37

1 Answers1

1
  1. You don't. You already import the class, therefore you should not require the fully qualified name.

  2. Don't re-parameterize your overridden method. Just remove the type parameter from the method signature. Note its absence in the below snippet.

    @Override
    public ParamConverter<MyDate> getConverter(final Class<MyDate> rawType, Type genericType, Annotation[] annotations) {
    
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Nope for 1. Thats what i expected , interestingly that import makes 0 difference.( anywhere in the class). You can try it , just need to create a MyDate class with those 3 integer fields. and you can remove the type for 2 , the compiler wont be able to find the overridden method. – Rpant Dec 30 '18 at 07:25
  • @Rpant: Did you not follow #2 as well as #1? Those do go hand in hand. – Makoto Dec 30 '18 at 07:53
  • If i try both together , it solves the problem of having to write fully qualified name of `MyDate`, but now the compiler can't seem to find an implementation of `getConvertor` with the signature as defined in question. Since one ParamConvertorProvider provides just one ParamConvertor , I wonder why the designers of JAX-RS did not provide the type T to the class , insteasd of method. – Rpant Dec 31 '18 at 04:13